
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>test · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="RibbitMQ.html">
            
                <a href="RibbitMQ.html">
            
                    
                    test
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >test</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="rabbitmq-&#x5B9E;&#x6218;&#x6559;&#x7A0B;">RabbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;</h1>
<h2 id="1mq&#x5F15;&#x8A00;">1.MQ&#x5F15;&#x8A00;</h2>
<h3 id="11-&#x4EC0;&#x4E48;&#x662F;mq">1.1 &#x4EC0;&#x4E48;&#x662F;MQ</h3>
<p><code>MQ</code>(Message Quene) :  &#x7FFB;&#x8BD1;&#x4E3A; <code>&#x6D88;&#x606F;&#x961F;&#x5217;</code>,&#x901A;&#x8FC7;&#x5178;&#x578B;&#x7684; <code>&#x751F;&#x4EA7;&#x8005;</code>&#x548C;<code>&#x6D88;&#x8D39;&#x8005;</code>&#x6A21;&#x578B;,&#x751F;&#x4EA7;&#x8005;&#x4E0D;&#x65AD;&#x5411;&#x6D88;&#x606F;&#x961F;&#x5217;&#x4E2D;&#x751F;&#x4EA7;&#x6D88;&#x606F;&#xFF0C;&#x6D88;&#x8D39;&#x8005;&#x4E0D;&#x65AD;&#x7684;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x6D88;&#x606F;&#x3002;&#x56E0;&#x4E3A;&#x6D88;&#x606F;&#x7684;&#x751F;&#x4EA7;&#x548C;&#x6D88;&#x8D39;&#x90FD;&#x662F;&#x5F02;&#x6B65;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x53EA;&#x5173;&#x5FC3;&#x6D88;&#x606F;&#x7684;&#x53D1;&#x9001;&#x548C;&#x63A5;&#x6536;&#xFF0C;&#x6CA1;&#x6709;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x7684;&#x4FB5;&#x5165;,&#x8F7B;&#x677E;&#x7684;&#x5B9E;&#x73B0;&#x7CFB;&#x7EDF;&#x95F4;&#x89E3;&#x8026;&#x3002;&#x522B;&#x540D;&#x4E3A; <code>&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;</code>    &#x901A;&#x8FC7;&#x5229;&#x7528;&#x9AD8;&#x6548;&#x53EF;&#x9760;&#x7684;&#x6D88;&#x606F;&#x4F20;&#x9012;&#x673A;&#x5236;&#x8FDB;&#x884C;&#x5E73;&#x53F0;&#x65E0;&#x5173;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x6D41;&#xFF0C;&#x5E76;&#x57FA;&#x4E8E;&#x6570;&#x636E;&#x901A;&#x4FE1;&#x6765;&#x8FDB;&#x884C;&#x5206;&#x5E03;&#x5F0F;&#x7CFB;&#x7EDF;&#x7684;&#x96C6;&#x6210;&#x3002;</p>
<h3 id="12-mq&#x6709;&#x54EA;&#x4E9B;">1.2 MQ&#x6709;&#x54EA;&#x4E9B;</h3>
<p>&#x5F53;&#x4ECA;&#x5E02;&#x9762;&#x4E0A;&#x6709;&#x5F88;&#x591A;&#x4E3B;&#x6D41;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5982;&#x8001;&#x724C;&#x7684;<code>ActiveMQ</code>&#x3001;<code>RabbitMQ</code>&#xFF0C;&#x7099;&#x624B;&#x53EF;&#x70ED;&#x7684;<code>Kafka</code>&#xFF0C;&#x963F;&#x91CC;&#x5DF4;&#x5DF4;&#x81EA;&#x4E3B;&#x5F00;&#x53D1;<code>RocketMQ</code>&#x7B49;&#x3002;</p>
<h3 id="13-&#x4E0D;&#x540C;mq&#x7279;&#x70B9;">1.3 &#x4E0D;&#x540C;MQ&#x7279;&#x70B9;</h3>
<pre><code class="lang-markdown"><span class="hljs-section"># 1.ActiveMQ</span>
<span class="hljs-code">        ActiveMQ &#x662F;Apache&#x51FA;&#x54C1;&#xFF0C;&#x6700;&#x6D41;&#x884C;&#x7684;&#xFF0C;&#x80FD;&#x529B;&#x5F3A;&#x52B2;&#x7684;&#x5F00;&#x6E90;&#x6D88;&#x606F;&#x603B;&#x7EBF;&#x3002;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x5168;&#x652F;&#x6301;JMS&#x89C4;&#x8303;&#x7684;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;&#x3002;&#x4E30;&#x5BCC;&#x7684;API,&#x591A;&#x79CD;&#x96C6;&#x7FA4;&#x67B6;&#x6784;&#x6A21;&#x5F0F;&#x8BA9;ActiveMQ&#x5728;&#x4E1A;&#x754C;&#x6210;&#x4E3A;&#x8001;&#x724C;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;,&#x5728;&#x4E2D;&#x5C0F;&#x578B;&#x4F01;&#x4E1A;&#x9887;&#x53D7;&#x6B22;&#x8FCE;!</span>

<span class="hljs-section"># 2.Kafka</span>
<span class="hljs-code">        Kafka&#x662F;LinkedIn&#x5F00;&#x6E90;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x53D1;&#x5E03;-&#x8BA2;&#x9605;&#x6D88;&#x606F;&#x7CFB;&#x7EDF;&#xFF0C;&#x76EE;&#x524D;&#x5F52;&#x5C5E;&#x4E8E;Apache&#x9876;&#x7EA7;&#x9879;&#x76EE;&#x3002;Kafka&#x4E3B;&#x8981;&#x7279;&#x70B9;&#x662F;&#x57FA;&#x4E8E;Pull&#x7684;&#x6A21;&#x5F0F;&#x6765;&#x5904;&#x7406;&#x6D88;&#x606F;&#x6D88;&#x8D39;&#xFF0C;</span>
<span class="hljs-code">        &#x8FFD;&#x6C42;&#x9AD8;&#x541E;&#x5410;&#x91CF;&#xFF0C;&#x4E00;&#x5F00;&#x59CB;&#x7684;&#x76EE;&#x7684;&#x5C31;&#x662F;&#x7528;&#x4E8E;&#x65E5;&#x5FD7;&#x6536;&#x96C6;&#x548C;&#x4F20;&#x8F93;&#x3002;0.8&#x7248;&#x672C;&#x5F00;&#x59CB;&#x652F;&#x6301;&#x590D;&#x5236;&#xFF0C;&#x4E0D;&#x652F;&#x6301;&#x4E8B;&#x52A1;&#xFF0C;&#x5BF9;&#x6D88;&#x606F;&#x7684;&#x91CD;&#x590D;&#x3001;&#x4E22;&#x5931;&#x3001;&#x9519;&#x8BEF;&#x6CA1;&#x6709;&#x4E25;&#x683C;&#x8981;&#x6C42;&#xFF0C;</span>
<span class="hljs-code">        &#x9002;&#x5408;&#x4EA7;&#x751F;&#x5927;&#x91CF;&#x6570;&#x636E;&#x7684;&#x4E92;&#x8054;&#x7F51;&#x670D;&#x52A1;&#x7684;&#x6570;&#x636E;&#x6536;&#x96C6;&#x4E1A;&#x52A1;&#x3002;</span>

<span class="hljs-section"># 3.RocketMQ</span>
<span class="hljs-code">        RocketMQ&#x662F;&#x963F;&#x91CC;&#x5F00;&#x6E90;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5B83;&#x662F;&#x7EAF;Java&#x5F00;&#x53D1;&#xFF0C;&#x5177;&#x6709;&#x9AD8;&#x541E;&#x5410;&#x91CF;&#x3001;&#x9AD8;&#x53EF;&#x7528;&#x6027;&#x3001;&#x9002;&#x5408;&#x5927;&#x89C4;&#x6A21;&#x5206;&#x5E03;&#x5F0F;&#x7CFB;&#x7EDF;&#x5E94;&#x7528;&#x7684;&#x7279;&#x70B9;&#x3002;RocketMQ&#x601D;&#x8DEF;&#x8D77;</span>
<span class="hljs-code">        &#x6E90;&#x4E8E;Kafka&#xFF0C;&#x4F46;&#x5E76;&#x4E0D;&#x662F;Kafka&#x7684;&#x4E00;&#x4E2A;Copy&#xFF0C;&#x5B83;&#x5BF9;&#x6D88;&#x606F;&#x7684;&#x53EF;&#x9760;&#x4F20;&#x8F93;&#x53CA;&#x4E8B;&#x52A1;&#x6027;&#x505A;&#x4E86;&#x4F18;&#x5316;&#xFF0C;&#x76EE;&#x524D;&#x5728;&#x963F;&#x91CC;&#x96C6;&#x56E2;&#x88AB;&#x5E7F;&#x6CDB;&#x5E94;&#x7528;&#x4E8E;&#x4EA4;&#x6613;&#x3001;&#x5145;&#x503C;&#x3001;&#x6D41;&#x8BA1;&#x7B97;&#x3001;&#x6D88;</span>
<span class="hljs-code">        &#x606F;&#x63A8;&#x9001;&#x3001;&#x65E5;&#x5FD7;&#x6D41;&#x5F0F;&#x5904;&#x7406;&#x3001;binglog&#x5206;&#x53D1;&#x7B49;&#x573A;&#x666F;&#x3002;</span>

<span class="hljs-section"># 4.RabbitMQ</span>
<span class="hljs-code">        RabbitMQ&#x662F;&#x4F7F;&#x7528;Erlang&#x8BED;&#x8A00;&#x5F00;&#x53D1;&#x7684;&#x5F00;&#x6E90;&#x6D88;&#x606F;&#x961F;&#x5217;&#x7CFB;&#x7EDF;&#xFF0C;&#x57FA;&#x4E8E;AMQP&#x534F;&#x8BAE;&#x6765;&#x5B9E;&#x73B0;&#x3002;AMQP&#x7684;&#x4E3B;&#x8981;&#x7279;&#x5F81;&#x662F;&#x9762;&#x5411;&#x6D88;&#x606F;&#x3001;&#x961F;&#x5217;&#x3001;&#x8DEF;&#x7531;&#xFF08;&#x5305;&#x62EC;&#x70B9;&#x5BF9;&#x70B9;&#x548C;</span>
<span class="hljs-code">        &#x53D1;&#x5E03;/&#x8BA2;&#x9605;&#xFF09;&#x3001;&#x53EF;&#x9760;&#x6027;&#x3001;&#x5B89;&#x5168;&#x3002;AMQP&#x534F;&#x8BAE;&#x66F4;&#x591A;&#x7528;&#x5728;&#x4F01;&#x4E1A;&#x7CFB;&#x7EDF;&#x5185;&#x5BF9;&#x6570;&#x636E;&#x4E00;&#x81F4;&#x6027;&#x3001;&#x7A33;&#x5B9A;&#x6027;&#x548C;&#x53EF;&#x9760;&#x6027;&#x8981;&#x6C42;&#x5F88;&#x9AD8;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x5BF9;&#x6027;&#x80FD;&#x548C;&#x541E;&#x5410;&#x91CF;&#x7684;&#x8981;&#x6C42;&#x8FD8;&#x5728;</span>
<span class="hljs-code">        &#x5176;&#x6B21;&#x3002;</span>
</code></pre>
<blockquote>
<p>RabbitMQ&#x6BD4;Kafka&#x53EF;&#x9760;&#xFF0C;Kafka&#x66F4;&#x9002;&#x5408;IO&#x9AD8;&#x541E;&#x5410;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x4E00;&#x822C;&#x5E94;&#x7528;&#x5728;&#x5927;&#x6570;&#x636E;&#x65E5;&#x5FD7;&#x5904;&#x7406;&#x6216;&#x5BF9;&#x5B9E;&#x65F6;&#x6027;&#xFF08;&#x5C11;&#x91CF;&#x5EF6;&#x8FDF;&#xFF09;&#xFF0C;&#x53EF;&#x9760;&#x6027;&#xFF08;&#x5C11;&#x91CF;&#x4E22;&#x6570;&#x636E;&#xFF09;&#x8981;&#x6C42;&#x7A0D;&#x4F4E;&#x7684;&#x573A;&#x666F;&#x4F7F;&#x7528;&#xFF0C;&#x6BD4;&#x5982;ELK&#x65E5;&#x5FD7;&#x6536;&#x96C6;&#x3002;</p>
</blockquote>
<hr>
<h2 id="2rabbitmq-&#x7684;&#x5F15;&#x8A00;">2.RabbitMQ &#x7684;&#x5F15;&#x8A00;</h2>
<h3 id="21-rabbitmq">2.1 RabbitMQ</h3>
<blockquote>
<p>&#x57FA;&#x4E8E;<code>AMQP</code>&#x534F;&#x8BAE;&#xFF0C;erlang&#x8BED;&#x8A00;&#x5F00;&#x53D1;&#xFF0C;&#x662F;&#x90E8;&#x7F72;&#x6700;&#x5E7F;&#x6CDB;&#x7684;&#x5F00;&#x6E90;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;,&#x662F;&#x6700;&#x53D7;&#x6B22;&#x8FCE;&#x7684;&#x5F00;&#x6E90;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;&#x4E4B;&#x4E00;&#x3002;</p>
</blockquote>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190925215603036-9419777.png" alt="image-20190925215603036"></p>
<p><code>&#x5B98;&#x7F51;</code>: <a href="https://www.rabbitmq.com/" target="_blank">https://www.rabbitmq.com/</a></p>
<p><code>&#x5B98;&#x65B9;&#x6559;&#x7A0B;</code>: <a href="https://www.rabbitmq.com/#getstarted" target="_blank">https://www.rabbitmq.com/#getstarted</a></p>
<pre><code class="lang-markdown"> # AMQP &#x534F;&#x8BAE;
<span class="hljs-code">         AMQP&#xFF08;advanced message queuing protocol&#xFF09;`&#x5728;2003&#x5E74;&#x65F6;&#x88AB;&#x63D0;&#x51FA;&#xFF0C;&#x6700;&#x65E9;&#x7528;&#x4E8E;&#x89E3;&#x51B3;&#x91D1;&#x878D;&#x9886;&#x4E0D;&#x540C;&#x5E73;&#x53F0;&#x4E4B;&#x95F4;&#x7684;&#x6D88;&#x606F;&#x4F20;&#x9012;&#x4EA4;&#x4E92;&#x95EE;&#x9898;&#x3002;&#x987E;&#x540D;&#x601D;&#x4E49;&#xFF0C;AMQP&#x662F;&#x4E00;&#x79CD;&#x534F;&#x8BAE;&#xFF0C;&#x66F4;&#x51C6;&#x786E;&#x7684;&#x8BF4;&#x662F;&#x4E00;&#x79CD;binary wire-level protocol&#xFF08;&#x94FE;&#x63A5;&#x534F;&#x8BAE;&#xFF09;&#x3002;&#x8FD9;&#x662F;&#x5176;&#x548C;JMS&#x7684;&#x672C;&#x8D28;&#x5DEE;&#x522B;&#xFF0C;AMQP&#x4E0D;&#x4ECE;API&#x5C42;&#x8FDB;&#x884C;&#x9650;&#x5B9A;&#xFF0C;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x5B9A;&#x4E49;&#x7F51;&#x7EDC;&#x4EA4;&#x6362;&#x7684;&#x6570;&#x636E;&#x683C;&#x5F0F;&#x3002;&#x8FD9;&#x4F7F;&#x5F97;&#x5B9E;&#x73B0;&#x4E86;AMQP&#x7684;provider&#x5929;&#x7136;&#x6027;&#x5C31;&#x662F;&#x8DE8;&#x5E73;&#x53F0;&#x7684;&#x3002;&#x4EE5;&#x4E0B;&#x662F;AMQP&#x534F;&#x8BAE;&#x6A21;&#x578B;:</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200311182438041.png" alt="image-20200311182438041"></p>
<h3 id="22-rabbitmq-&#x7684;&#x5B89;&#x88C5;">2.2 RabbitMQ &#x7684;&#x5B89;&#x88C5;</h3>
<h4 id="221-&#x4E0B;&#x8F7D;">2.2.1 &#x4E0B;&#x8F7D;</h4>
<p><code>&#x5B98;&#x7F51;&#x4E0B;&#x8F7D;&#x5730;&#x5740;</code>: <a href="https://www.rabbitmq.com/download.html![image-20190925220115235](RibbitMQ" target="_blank">https://www.rabbitmq.com/download.html![image-20190925220115235](RibbitMQ</a> &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190925220115235.png)</p>
<blockquote>
<p><code>&#x6700;&#x65B0;&#x7248;&#x672C;</code>: 3.7.18</p>
</blockquote>
<h4 id="222-&#x4E0B;&#x8F7D;&#x7684;&#x5B89;&#x88C5;&#x5305;">2.2.2 &#x4E0B;&#x8F7D;&#x7684;&#x5B89;&#x88C5;&#x5305;<img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190925220343521.png" alt="image-20190925220343521"></h4>
<blockquote>
<p><code>&#x6CE8;&#x610F;</code>:&#x8FD9;&#x91CC;&#x7684;&#x5B89;&#x88C5;&#x5305;&#x662F;centos7&#x5B89;&#x88C5;&#x7684;&#x5305;</p>
</blockquote>
<h4 id="223-&#x5B89;&#x88C5;&#x6B65;&#x9AA4;">2.2.3 &#x5B89;&#x88C5;&#x6B65;&#x9AA4;</h4>
<pre><code class="lang-markdown"><span class="hljs-section"># 1.&#x5C06;rabbitmq&#x5B89;&#x88C5;&#x5305;&#x4E0A;&#x4F20;&#x5230;linux&#x7CFB;&#x7EDF;&#x4E2D;</span>
<span class="hljs-code">    erlang-22.0.7-1.el7.x86_64.rpm</span>
<span class="hljs-code">    rabbitmq-server-3.7.18-1.el7.noarch.rpm</span>

<span class="hljs-section"># 2.&#x5B89;&#x88C5;Erlang&#x4F9D;&#x8D56;&#x5305;</span>
<span class="hljs-code">    rpm -ivh erlang-22.0.7-1.el7.x86_64.rpm</span>

<span class="hljs-section"># 3.&#x5B89;&#x88C5;RabbitMQ&#x5B89;&#x88C5;&#x5305;(&#x9700;&#x8981;&#x8054;&#x7F51;)</span>
<span class="hljs-code">    yum install -y rabbitmq-server-3.7.18-1.el7.noarch.rpm</span>
<span class="hljs-code">        &#x6CE8;&#x610F;:&#x9ED8;&#x8BA4;&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x540E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x6A21;&#x677F;&#x5728;:/usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example&#x76EE;&#x5F55;&#x4E2D;,&#x9700;&#x8981;    </span>
<span class="hljs-code">                &#x5C06;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x590D;&#x5236;&#x5230;/etc/rabbitmq/&#x76EE;&#x5F55;&#x4E2D;,&#x5E76;&#x4FEE;&#x6539;&#x540D;&#x79F0;&#x4E3A;rabbitmq.config</span>
<span class="hljs-section"># 4.&#x590D;&#x5236;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span>
<span class="hljs-code">    cp /usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config</span>

<span class="hljs-section"># 5.&#x67E5;&#x770B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4F4D;&#x7F6E;</span>
<span class="hljs-code">    ls /etc/rabbitmq/rabbitmq.config</span>

<span class="hljs-section"># 6.&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;(&#x53C2;&#x89C1;&#x4E0B;&#x56FE;:)</span>
<span class="hljs-code">    vim /etc/rabbitmq/rabbitmq.config</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190925222230260-3836271.png" alt="image-20190925222230260"></p>
<p>&#x5C06;&#x4E0A;&#x56FE;&#x4E2D;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x7EA2;&#x8272;&#x90E8;&#x5206;&#x53BB;&#x6389;<code>%%</code>,&#x4EE5;&#x53CA;&#x6700;&#x540E;&#x7684;<code>,</code>&#x9017;&#x53F7; &#x4FEE;&#x6539;&#x4E3A;&#x4E0B;&#x56FE;:</p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190925222329200-3836312.png" alt="image-20190925222329200"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># 7.&#x6267;&#x884C;&#x5982;&#x4E0B;&#x547D;&#x4EE4;,&#x542F;&#x52A8;rabbitmq&#x4E2D;&#x7684;&#x63D2;&#x4EF6;&#x7BA1;&#x7406;</span>
<span class="hljs-code">    rabbitmq-plugs enable rabbitmq_management</span>

<span class="hljs-code">    &#x51FA;&#x73B0;&#x5982;&#x4E0B;&#x8BF4;&#x660E;:</span>
<span class="hljs-code">        Enabling plugins on node rabbit@localhost:</span>
<span class="hljs-code">    rabbitmq_management</span>
<span class="hljs-code">    The following plugins have been configured:</span>
<span class="hljs-code">      rabbitmq_management</span>
<span class="hljs-code">      rabbitmq_management_agent</span>
<span class="hljs-code">      rabbitmq_web_dispatch</span>
<span class="hljs-code">    Applying plugin configuration to rabbit@localhost...</span>
<span class="hljs-code">    The following plugins have been enabled:</span>
<span class="hljs-code">      rabbitmq_management</span>
<span class="hljs-code">      rabbitmq_management_agent</span>
<span class="hljs-code">      rabbitmq_web_dispatch</span>

<span class="hljs-code">    set 3 plugins.</span>
<span class="hljs-code">    Offline change; changes will take effect at broker restart.</span>

<span class="hljs-section"># 8.&#x542F;&#x52A8;RabbitMQ&#x7684;&#x670D;&#x52A1;</span>
<span class="hljs-code">    systemctl start rabbitmq-server</span>
<span class="hljs-code">    systemctl restart rabbitmq-server</span>
<span class="hljs-code">    systemctl stop rabbitmq-server</span>


<span class="hljs-section"># 9.&#x67E5;&#x770B;&#x670D;&#x52A1;&#x72B6;&#x6001;(&#x89C1;&#x4E0B;&#x56FE;:)</span>
<span class="hljs-code">    systemctl status rabbitmq-server</span>
  &#x25CF; rabbitmq-server.service - RabbitMQ broker
<span class="hljs-code">     Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)</span>
<span class="hljs-code">     Active: active (running) since &#x4E09; 2019-09-25 22:26:35 CST; 7s ago</span>
   Main PID: 2904 (beam.smp)
<span class="hljs-code">     Status: &quot;Initialized&quot;</span>
<span class="hljs-code">     CGroup: /system.slice/rabbitmq-server.service</span>
<span class="hljs-code">             &#x251C;&#x2500;2904 /usr/lib64/erlang/erts-10.4.4/bin/beam.smp -W w -A 64 -MBas ageffcbf -MHas ageffcbf -</span>
<span class="hljs-code">             MBlmbcs...</span>
<span class="hljs-code">             &#x251C;&#x2500;3220 erl_child_setup 32768</span>
<span class="hljs-code">             &#x251C;&#x2500;3243 inet_gethost 4</span>
<span class="hljs-code">             &#x2514;&#x2500;3244 inet_gethost 4</span>
<span class="hljs-code">      .........</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190925222743776-3836511.png" alt="image-20190925222743776"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># 10.&#x5173;&#x95ED;&#x9632;&#x706B;&#x5899;&#x670D;&#x52A1;</span>
<span class="hljs-code">    systemctl disable firewalld</span>
<span class="hljs-code">    Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.</span>
<span class="hljs-code">    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.</span>
<span class="hljs-code">    systemctl stop firewalld   </span>

<span class="hljs-section"># 11.&#x8BBF;&#x95EE;web&#x7BA1;&#x7406;&#x754C;&#x9762;</span>
<span class="hljs-code">    http://10.15.0.8:15672/</span>
</code></pre>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190926194738708-3836601.png" alt="image-20190926194738708"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># 12.&#x767B;&#x5F55;&#x7BA1;&#x7406;&#x754C;&#x9762;</span>
<span class="hljs-code">    username:  guest</span>
<span class="hljs-code">    password:  guest</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20190926194954822-3836665.png" alt="image-20190926194954822"></p>
<hr>
<h2 id="3-rabiitmq-&#x914D;&#x7F6E;">3. RabiitMQ &#x914D;&#x7F6E;</h2>
<h3 id="31rabbitmq-&#x7BA1;&#x7406;&#x547D;&#x4EE4;&#x884C;">3.1RabbitMQ &#x7BA1;&#x7406;&#x547D;&#x4EE4;&#x884C;</h3>
<pre><code class="lang-markdown"><span class="hljs-section"># 1.&#x670D;&#x52A1;&#x542F;&#x52A8;&#x76F8;&#x5173;</span>
<span class="hljs-code">    systemctl start|restart|stop|status rabbitmq-server</span>

<span class="hljs-section"># 2.&#x7BA1;&#x7406;&#x547D;&#x4EE4;&#x884C;  &#x7528;&#x6765;&#x5728;&#x4E0D;&#x4F7F;&#x7528;web&#x7BA1;&#x7406;&#x754C;&#x9762;&#x60C5;&#x51B5;&#x4E0B;&#x547D;&#x4EE4;&#x64CD;&#x4F5C;RabbitMQ</span>
<span class="hljs-code">    rabbitmqctl  help  &#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x66F4;&#x591A;&#x547D;&#x4EE4;</span>

<span class="hljs-section"># 3.&#x63D2;&#x4EF6;&#x7BA1;&#x7406;&#x547D;&#x4EE4;&#x884C;</span>
<span class="hljs-code">    rabbitmq-plugins enable|list|disable</span>
</code></pre>
<h3 id="32-web&#x7BA1;&#x7406;&#x754C;&#x9762;&#x4ECB;&#x7ECD;">3.2 web&#x7BA1;&#x7406;&#x754C;&#x9762;&#x4ECB;&#x7ECD;</h3>
<h4 id="321-overview&#x6982;&#x89C8;">3.2.1 overview&#x6982;&#x89C8;</h4>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126162026720.png" alt="image-20191126162026720"></p>
<ul>
<li><p><code>connections&#xFF1A;&#x65E0;&#x8BBA;&#x751F;&#x4EA7;&#x8005;&#x8FD8;&#x662F;&#x6D88;&#x8D39;&#x8005;&#xFF0C;&#x90FD;&#x9700;&#x8981;&#x4E0E;RabbitMQ&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x540E;&#x624D;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x6D88;&#x606F;&#x7684;&#x751F;&#x4EA7;&#x548C;&#x6D88;&#x8D39;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x8FDE;&#x63A5;&#x60C5;&#x51B5;</code></p>
</li>
<li><p><code>channels&#xFF1A;&#x901A;&#x9053;&#xFF0C;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x540E;&#xFF0C;&#x4F1A;&#x5F62;&#x6210;&#x901A;&#x9053;&#xFF0C;&#x6D88;&#x606F;&#x7684;&#x6295;&#x9012;&#x83B7;&#x53D6;&#x4F9D;&#x8D56;&#x901A;&#x9053;&#x3002;</code></p>
</li>
<li><p><code>Exchanges&#xFF1A;&#x4EA4;&#x6362;&#x673A;&#xFF0C;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x6D88;&#x606F;&#x7684;&#x8DEF;&#x7531;</code></p>
</li>
<li><p><code>Queues&#xFF1A;&#x961F;&#x5217;&#xFF0C;&#x5373;&#x6D88;&#x606F;&#x961F;&#x5217;&#xFF0C;&#x6D88;&#x606F;&#x5B58;&#x653E;&#x5728;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x7B49;&#x5F85;&#x6D88;&#x8D39;&#xFF0C;&#x6D88;&#x8D39;&#x540E;&#x88AB;&#x79FB;&#x9664;&#x961F;&#x5217;&#x3002;</code></p>
</li>
</ul>
<h4 id="322-admin&#x7528;&#x6237;&#x548C;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#x7BA1;&#x7406;">3.2.2 Admin&#x7528;&#x6237;&#x548C;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#x7BA1;&#x7406;</h4>
<h5 id="1-&#x6DFB;&#x52A0;&#x7528;&#x6237;">1. &#x6DFB;&#x52A0;&#x7528;&#x6237;</h5>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126162617280.png" alt="image-20191126162617280"></p>
<p>&#x4E0A;&#x9762;&#x7684;Tags&#x9009;&#x9879;&#xFF0C;&#x5176;&#x5B9E;&#x662F;&#x6307;&#x5B9A;&#x7528;&#x6237;&#x7684;&#x89D2;&#x8272;&#xFF0C;&#x53EF;&#x9009;&#x7684;&#x6709;&#x4EE5;&#x4E0B;&#x51E0;&#x4E2A;&#xFF1A;</p>
<ul>
<li><p><code>&#x8D85;&#x7EA7;&#x7BA1;&#x7406;&#x5458;(administrator)</code></p>
<p>&#x53EF;&#x767B;&#x9646;&#x7BA1;&#x7406;&#x63A7;&#x5236;&#x53F0;&#xFF0C;&#x53EF;&#x67E5;&#x770B;&#x6240;&#x6709;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x5BF9;&#x7528;&#x6237;&#xFF0C;&#x7B56;&#x7565;(policy)&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p>
</li>
<li><p><code>&#x76D1;&#x63A7;&#x8005;(monitoring)</code></p>
<p>&#x53EF;&#x767B;&#x9646;&#x7BA1;&#x7406;&#x63A7;&#x5236;&#x53F0;&#xFF0C;&#x540C;&#x65F6;&#x53EF;&#x4EE5;&#x67E5;&#x770B;rabbitmq&#x8282;&#x70B9;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;(&#x8FDB;&#x7A0B;&#x6570;&#xFF0C;&#x5185;&#x5B58;&#x4F7F;&#x7528;&#x60C5;&#x51B5;&#xFF0C;&#x78C1;&#x76D8;&#x4F7F;&#x7528;&#x60C5;&#x51B5;&#x7B49;)</p>
</li>
<li><p><code>&#x7B56;&#x7565;&#x5236;&#x5B9A;&#x8005;(policymaker)</code></p>
<p>&#x53EF;&#x767B;&#x9646;&#x7BA1;&#x7406;&#x63A7;&#x5236;&#x53F0;, &#x540C;&#x65F6;&#x53EF;&#x4EE5;&#x5BF9;policy&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;&#x4F46;&#x65E0;&#x6CD5;&#x67E5;&#x770B;&#x8282;&#x70B9;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;(&#x4E0A;&#x56FE;&#x7EA2;&#x6846;&#x6807;&#x8BC6;&#x7684;&#x90E8;&#x5206;)&#x3002;</p>
</li>
<li><p><code>&#x666E;&#x901A;&#x7BA1;&#x7406;&#x8005;(management)</code></p>
<p>&#x4EC5;&#x53EF;&#x767B;&#x9646;&#x7BA1;&#x7406;&#x63A7;&#x5236;&#x53F0;&#xFF0C;&#x65E0;&#x6CD5;&#x770B;&#x5230;&#x8282;&#x70B9;&#x4FE1;&#x606F;&#xFF0C;&#x4E5F;&#x65E0;&#x6CD5;&#x5BF9;&#x7B56;&#x7565;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;</p>
</li>
<li><p><code>&#x5176;&#x4ED6;</code></p>
<p>&#x65E0;&#x6CD5;&#x767B;&#x9646;&#x7BA1;&#x7406;&#x63A7;&#x5236;&#x53F0;&#xFF0C;&#x901A;&#x5E38;&#x5C31;&#x662F;&#x666E;&#x901A;&#x7684;&#x751F;&#x4EA7;&#x8005;&#x548C;&#x6D88;&#x8D39;&#x8005;&#x3002;</p>
</li>
</ul>
<h5 id="2-&#x521B;&#x5EFA;&#x865A;&#x62DF;&#x4E3B;&#x673A;">2. &#x521B;&#x5EFA;&#x865A;&#x62DF;&#x4E3B;&#x673A;</h5>
<pre><code class="lang-markdown"><span class="hljs-section"># &#x865A;&#x62DF;&#x4E3B;&#x673A;</span>
<span class="hljs-code">    &#x4E3A;&#x4E86;&#x8BA9;&#x5404;&#x4E2A;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x4E92;&#x4E0D;&#x5E72;&#x6270;&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;RabbitMQ&#x6DFB;&#x52A0;&#x4E86;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#xFF08;Virtual Hosts&#xFF09;&#x7684;&#x6982;&#x5FF5;&#x3002;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x8BBF;&#x95EE;&#x8DEF;&#x5F84;&#xFF0C;&#x4E0D;&#x540C;&#x7528;&#x6237;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x8DEF;&#x5F84;&#xFF0C;&#x5404;&#x81EA;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x961F;&#x5217;&#x3001;&#x4EA4;&#x6362;&#x673A;&#xFF0C;&#x4E92;&#x76F8;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x3002;</span>
</code></pre>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126163023153.png" alt="image-20191126163023153"></p>
<h5 id="3-&#x7ED1;&#x5B9A;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#x548C;&#x7528;&#x6237;">3. &#x7ED1;&#x5B9A;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#x548C;&#x7528;&#x6237;</h5>
<p>&#x521B;&#x5EFA;&#x597D;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x8981;&#x7ED9;&#x7528;&#x6237;&#x6DFB;&#x52A0;&#x8BBF;&#x95EE;&#x6743;&#x9650;&#xFF1A;</p>
<p>&#x70B9;&#x51FB;&#x6DFB;&#x52A0;&#x597D;&#x7684;&#x865A;&#x62DF;&#x4E3B;&#x673A;&#xFF1A;</p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126163506795.png" alt="image-20191126163506795"></p>
<p>&#x8FDB;&#x5165;&#x865A;&#x62DF;&#x673A;&#x8BBE;&#x7F6E;&#x754C;&#x9762;:</p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126163631889.png" alt="image-20191126163631889"></p>
<hr>
<h2 id="4rabbitmq-&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;">4.RabbitMQ &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;</h2>
<h3 id="40-amqp&#x534F;&#x8BAE;&#x7684;&#x56DE;&#x987E;">4.0 AMQP&#x534F;&#x8BAE;&#x7684;&#x56DE;&#x987E;</h3>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200312140114784.png" alt="image-20200312140114784"></p>
<h3 id="41-rabbitmq&#x652F;&#x6301;&#x7684;&#x6D88;&#x606F;&#x6A21;&#x578B;">4.1 RabbitMQ&#x652F;&#x6301;&#x7684;&#x6D88;&#x606F;&#x6A21;&#x578B;</h3>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126165434784.png" alt="image-20191126165434784"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126165459282.png" alt="image-20191126165459282"></p>
<h3 id="42-&#x5F15;&#x5165;&#x4F9D;&#x8D56;">4.2 &#x5F15;&#x5165;&#x4F9D;&#x8D56;</h3>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.rabbitmq<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>amqp-client<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.7.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<h3 id="43-&#x7B2C;&#x4E00;&#x79CD;&#x6A21;&#x578B;&#x76F4;&#x8FDE;">4.3 &#x7B2C;&#x4E00;&#x79CD;&#x6A21;&#x578B;(&#x76F4;&#x8FDE;)</h3>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126165840602.png" alt="image-20191126165840602"></p>
<p>&#x5728;&#x4E0A;&#x56FE;&#x7684;&#x6A21;&#x578B;&#x4E2D;&#xFF0C;&#x6709;&#x4EE5;&#x4E0B;&#x6982;&#x5FF5;&#xFF1A;</p>
<ul>
<li>P&#xFF1A;&#x751F;&#x4EA7;&#x8005;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8981;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7684;&#x7A0B;&#x5E8F;</li>
<li>C&#xFF1A;&#x6D88;&#x8D39;&#x8005;&#xFF1A;&#x6D88;&#x606F;&#x7684;&#x63A5;&#x53D7;&#x8005;&#xFF0C;&#x4F1A;&#x4E00;&#x76F4;&#x7B49;&#x5F85;&#x6D88;&#x606F;&#x5230;&#x6765;&#x3002;</li>
<li>queue&#xFF1A;&#x6D88;&#x606F;&#x961F;&#x5217;&#xFF0C;&#x56FE;&#x4E2D;&#x7EA2;&#x8272;&#x90E8;&#x5206;&#x3002;&#x7C7B;&#x4F3C;&#x4E00;&#x4E2A;&#x90AE;&#x7BB1;&#xFF0C;&#x53EF;&#x4EE5;&#x7F13;&#x5B58;&#x6D88;&#x606F;&#xFF1B;&#x751F;&#x4EA7;&#x8005;&#x5411;&#x5176;&#x4E2D;&#x6295;&#x9012;&#x6D88;&#x606F;&#xFF0C;&#x6D88;&#x8D39;&#x8005;&#x4ECE;&#x5176;&#x4E2D;&#x53D6;&#x51FA;&#x6D88;&#x606F;&#x3002;</li>
</ul>
<h5 id="1-&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">1. &#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java">  <span class="hljs-comment">//&#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#x5DE5;&#x5382;</span>
  ConnectionFactory connectionFactory = <span class="hljs-keyword">new</span> ConnectionFactory();
  connectionFactory.setHost(<span class="hljs-string">&quot;10.15.0.9&quot;</span>);
  connectionFactory.setPort(<span class="hljs-number">5672</span>);
  connectionFactory.setUsername(<span class="hljs-string">&quot;ems&quot;</span>);
  connectionFactory.setPassword(<span class="hljs-string">&quot;123&quot;</span>);
  connectionFactory.setVirtualHost(<span class="hljs-string">&quot;/ems&quot;</span>);
  Connection connection = connectionFactory.newConnection();
  <span class="hljs-comment">//&#x521B;&#x5EFA;&#x901A;&#x9053;</span>
  Channel channel = connection.createChannel();
  <span class="hljs-comment">//&#x53C2;&#x6570;1: &#x662F;&#x5426;&#x6301;&#x4E45;&#x5316;  &#x53C2;&#x6570;2:&#x662F;&#x5426;&#x72EC;&#x5360;&#x961F;&#x5217; &#x53C2;&#x6570;3:&#x662F;&#x5426;&#x81EA;&#x52A8;&#x5220;&#x9664;  &#x53C2;&#x6570;4:&#x5176;&#x4ED6;&#x5C5E;&#x6027;</span>
  channel.queueDeclare(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">null</span>);
  channel.basicPublish(<span class="hljs-string">&quot;&quot;</span>,<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-keyword">null</span>,<span class="hljs-string">&quot;hello rabbitmq&quot;</span>.getBytes());
  channel.close();
  connection.close();
</code></pre>
<h5 id="2-&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;">2. &#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;</h5>
<pre><code class="lang-java">  <span class="hljs-comment">//&#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#x5DE5;&#x5382;</span>
  ConnectionFactory connectionFactory = <span class="hljs-keyword">new</span> ConnectionFactory();
  connectionFactory.setHost(<span class="hljs-string">&quot;10.15.0.9&quot;</span>);
  connectionFactory.setPort(<span class="hljs-number">5672</span>);
  connectionFactory.setUsername(<span class="hljs-string">&quot;ems&quot;</span>);
  connectionFactory.setPassword(<span class="hljs-string">&quot;123&quot;</span>);
  connectionFactory.setVirtualHost(<span class="hljs-string">&quot;/ems&quot;</span>);
  Connection connection = connectionFactory.newConnection();
  Channel channel = connection.createChannel();
  channel.queueDeclare(<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">null</span>);
  channel.basicConsume(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
      System.out.println(<span class="hljs-keyword">new</span> String(body));
    }
  });
</code></pre>
<h5 id="3-&#x53C2;&#x6570;&#x7684;&#x8BF4;&#x660E;">3. &#x53C2;&#x6570;&#x7684;&#x8BF4;&#x660E;</h5>
<pre><code class="lang-java">  channel.queueDeclare(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">null</span>);
    <span class="hljs-string">&apos;&#x53C2;&#x6570;1&apos;</span>:&#x7528;&#x6765;&#x58F0;&#x660E;&#x901A;&#x9053;&#x5BF9;&#x5E94;&#x7684;&#x961F;&#x5217;
  <span class="hljs-string">&apos;&#x53C2;&#x6570;2&apos;</span>:&#x7528;&#x6765;&#x6307;&#x5B9A;&#x662F;&#x5426;&#x6301;&#x4E45;&#x5316;&#x961F;&#x5217;
  <span class="hljs-string">&apos;&#x53C2;&#x6570;3&apos;</span>:&#x7528;&#x6765;&#x6307;&#x5B9A;&#x662F;&#x5426;&#x72EC;&#x5360;&#x961F;&#x5217;
  <span class="hljs-string">&apos;&#x53C2;&#x6570;4&apos;</span>:&#x7528;&#x6765;&#x6307;&#x5B9A;&#x662F;&#x5426;&#x81EA;&#x52A8;&#x5220;&#x9664;&#x961F;&#x5217;
  <span class="hljs-string">&apos;&#x53C2;&#x6570;5&apos;</span>:&#x5BF9;&#x961F;&#x5217;&#x7684;&#x989D;&#x5916;&#x914D;&#x7F6E;
</code></pre>
<hr>
<h3 id="44-&#x7B2C;&#x4E8C;&#x79CD;&#x6A21;&#x578B;work-quene">4.4 &#x7B2C;&#x4E8C;&#x79CD;&#x6A21;&#x578B;(work quene)</h3>
<p><code>Work queues</code>&#xFF0C;&#x4E5F;&#x88AB;&#x79F0;&#x4E3A;&#xFF08;<code>Task queues</code>&#xFF09;&#xFF0C;&#x4EFB;&#x52A1;&#x6A21;&#x578B;&#x3002;&#x5F53;&#x6D88;&#x606F;&#x5904;&#x7406;&#x6BD4;&#x8F83;&#x8017;&#x65F6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x80FD;&#x751F;&#x4EA7;&#x6D88;&#x606F;&#x7684;&#x901F;&#x5EA6;&#x4F1A;&#x8FDC;&#x8FDC;&#x5927;&#x4E8E;&#x6D88;&#x606F;&#x7684;&#x6D88;&#x8D39;&#x901F;&#x5EA6;&#x3002;&#x957F;&#x6B64;&#x4EE5;&#x5F80;&#xFF0C;&#x6D88;&#x606F;&#x5C31;&#x4F1A;&#x5806;&#x79EF;&#x8D8A;&#x6765;&#x8D8A;&#x591A;&#xFF0C;&#x65E0;&#x6CD5;&#x53CA;&#x65F6;&#x5904;&#x7406;&#x3002;&#x6B64;&#x65F6;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;work &#x6A21;&#x578B;&#xFF1A;<strong>&#x8BA9;&#x591A;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x7ED1;&#x5B9A;&#x5230;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#xFF0C;&#x5171;&#x540C;&#x6D88;&#x8D39;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6D88;&#x606F;</strong>&#x3002;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6D88;&#x606F;&#x4E00;&#x65E6;&#x6D88;&#x8D39;&#xFF0C;&#x5C31;&#x4F1A;&#x6D88;&#x5931;&#xFF0C;&#x56E0;&#x6B64;&#x4EFB;&#x52A1;&#x662F;&#x4E0D;&#x4F1A;&#x88AB;&#x91CD;&#x590D;&#x6267;&#x884C;&#x7684;&#x3002;</p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200314221002008.png" alt="image-20200314221002008"></p>
<p>&#x89D2;&#x8272;&#xFF1A;</p>
<ul>
<li>P&#xFF1A;&#x751F;&#x4EA7;&#x8005;&#xFF1A;&#x4EFB;&#x52A1;&#x7684;&#x53D1;&#x5E03;&#x8005;</li>
<li>C1&#xFF1A;&#x6D88;&#x8D39;&#x8005;-1&#xFF0C;&#x9886;&#x53D6;&#x4EFB;&#x52A1;&#x5E76;&#x4E14;&#x5B8C;&#x6210;&#x4EFB;&#x52A1;&#xFF0C;&#x5047;&#x8BBE;&#x5B8C;&#x6210;&#x901F;&#x5EA6;&#x8F83;&#x6162;</li>
<li>C2&#xFF1A;&#x6D88;&#x8D39;&#x8005;-2&#xFF1A;&#x9886;&#x53D6;&#x4EFB;&#x52A1;&#x5E76;&#x5B8C;&#x6210;&#x4EFB;&#x52A1;&#xFF0C;&#x5047;&#x8BBE;&#x5B8C;&#x6210;&#x901F;&#x5EA6;&#x5FEB;</li>
</ul>
<h5 id="1-&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">1. &#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java">channel.queueDeclare(<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">null</span>);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
  channel.basicPublish(<span class="hljs-string">&quot;&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-keyword">null</span>, (i+<span class="hljs-string">&quot;====&gt;:&#x6211;&#x662F;&#x6D88;&#x606F;&quot;</span>).getBytes());
}
</code></pre>
<h5 id="2&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1">2.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1</h5>
<pre><code class="lang-java">channel.queueDeclare(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">null</span>);
channel.basicConsume(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;1: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="3&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2">3.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2</h5>
<pre><code class="lang-java">channel.queueDeclare(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">null</span>);
channel.basicConsume(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    <span class="hljs-keyword">try</span> {
      Thread.sleep(<span class="hljs-number">1000</span>);   <span class="hljs-comment">//&#x5904;&#x7406;&#x6D88;&#x606F;&#x6BD4;&#x8F83;&#x6162; &#x4E00;&#x79D2;&#x5904;&#x7406;&#x4E00;&#x4E2A;&#x6D88;&#x606F;</span>
    } <span class="hljs-keyword">catch</span> (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;2: &quot;</span>+<span class="hljs-keyword">new</span> String(body));  
  }
});
</code></pre>
<h5 id="4&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;">4.&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;</h5>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200314223242058.png" alt="image-20200314223242058"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200314223302207.png" alt="image-20200314223302207"></p>
<blockquote>
<p><code>&#x603B;&#x7ED3;:&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;RabbitMQ&#x5C06;&#x6309;&#x987A;&#x5E8F;&#x5C06;&#x6BCF;&#x4E2A;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;&#x8005;&#x3002;&#x5E73;&#x5747;&#x800C;&#x8A00;&#xFF0C;&#x6BCF;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x90FD;&#x4F1A;&#x6536;&#x5230;&#x76F8;&#x540C;&#x6570;&#x91CF;&#x7684;&#x6D88;&#x606F;&#x3002;&#x8FD9;&#x79CD;&#x5206;&#x53D1;&#x6D88;&#x606F;&#x7684;&#x65B9;&#x5F0F;&#x79F0;&#x4E3A;&#x5FAA;&#x73AF;&#x3002;</code></p>
</blockquote>
<h5 id="5&#x6D88;&#x606F;&#x81EA;&#x52A8;&#x786E;&#x8BA4;&#x673A;&#x5236;">5.&#x6D88;&#x606F;&#x81EA;&#x52A8;&#x786E;&#x8BA4;&#x673A;&#x5236;</h5>
<blockquote>
<p>Doing a task can take a few seconds. You may wonder what happens if one of the consumers starts a long task and dies with it only partly done. With our current code, once RabbitMQ delivers a message to the consumer it immediately marks it for deletion. In this case, if you kill a worker we will lose the message it was just processing. We&apos;ll also lose all the messages that were dispatched to this particular worker but were not yet handled.</p>
<p>But we don&apos;t want to lose any tasks. If a worker dies, we&apos;d like the task to be delivered to another worker.</p>
</blockquote>
<pre><code class="lang-java">channel.basicQos(<span class="hljs-number">1</span>);<span class="hljs-comment">//&#x4E00;&#x6B21;&#x53EA;&#x63A5;&#x53D7;&#x4E00;&#x6761;&#x672A;&#x786E;&#x8BA4;&#x7684;&#x6D88;&#x606F;</span>
<span class="hljs-comment">//&#x53C2;&#x6570;2:&#x5173;&#x95ED;&#x81EA;&#x52A8;&#x786E;&#x8BA4;&#x6D88;&#x606F;</span>
channel.basicConsume(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-keyword">false</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;1: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
    channel.basicAck(envelope.getDeliveryTag(),<span class="hljs-keyword">false</span>);<span class="hljs-comment">//&#x624B;&#x52A8;&#x786E;&#x8BA4;&#x6D88;&#x606F;</span>
  }
});
</code></pre>
<ul>
<li><p>&#x8BBE;&#x7F6E;&#x901A;&#x9053;&#x4E00;&#x6B21;&#x53EA;&#x80FD;&#x6D88;&#x8D39;&#x4E00;&#x4E2A;&#x6D88;&#x606F;</p>
</li>
<li><p>&#x5173;&#x95ED;&#x6D88;&#x606F;&#x7684;&#x81EA;&#x52A8;&#x786E;&#x8BA4;,&#x5F00;&#x542F;&#x624B;&#x52A8;&#x786E;&#x8BA4;&#x6D88;&#x606F;</p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200314230412178.png" alt="image-20200314230412178"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200314230423280.png" alt="image-20200314230423280"></p>
</li>
</ul>
<hr>
<h3 id="45-&#x7B2C;&#x4E09;&#x79CD;&#x6A21;&#x578B;fanout">4.5 &#x7B2C;&#x4E09;&#x79CD;&#x6A21;&#x578B;(fanout)</h3>
<p><code>fanout &#x6247;&#x51FA; &#x4E5F;&#x79F0;&#x4E3A;&#x5E7F;&#x64AD;</code></p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126213115873.png" alt="image-20191126213115873"></p>
<p>&#x5728;&#x5E7F;&#x64AD;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x6D41;&#x7A0B;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<ul>
<li>&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x6D88;&#x8D39;&#x8005;</li>
<li>&#x6BCF;&#x4E2A;<strong>&#x6D88;&#x8D39;&#x8005;&#x6709;&#x81EA;&#x5DF1;&#x7684;queue</strong>&#xFF08;&#x961F;&#x5217;&#xFF09;</li>
<li>&#x6BCF;&#x4E2A;<strong>&#x961F;&#x5217;&#x90FD;&#x8981;&#x7ED1;&#x5B9A;&#x5230;Exchange</strong>&#xFF08;&#x4EA4;&#x6362;&#x673A;&#xFF09;</li>
<li><strong>&#x751F;&#x4EA7;&#x8005;&#x53D1;&#x9001;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x53EA;&#x80FD;&#x53D1;&#x9001;&#x5230;&#x4EA4;&#x6362;&#x673A;</strong>&#xFF0C;&#x4EA4;&#x6362;&#x673A;&#x6765;&#x51B3;&#x5B9A;&#x8981;&#x53D1;&#x7ED9;&#x54EA;&#x4E2A;&#x961F;&#x5217;&#xFF0C;&#x751F;&#x4EA7;&#x8005;&#x65E0;&#x6CD5;&#x51B3;&#x5B9A;&#x3002;</li>
<li>&#x4EA4;&#x6362;&#x673A;&#x628A;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7ED9;&#x7ED1;&#x5B9A;&#x8FC7;&#x7684;&#x6240;&#x6709;&#x961F;&#x5217;</li>
<li>&#x961F;&#x5217;&#x7684;&#x6D88;&#x8D39;&#x8005;&#x90FD;&#x80FD;&#x62FF;&#x5230;&#x6D88;&#x606F;&#x3002;&#x5B9E;&#x73B0;&#x4E00;&#x6761;&#x6D88;&#x606F;&#x88AB;&#x591A;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x6D88;&#x8D39;</li>
</ul>
<h5 id="1-&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">1. &#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x58F0;&#x660E;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;fanout&quot;</span>);<span class="hljs-comment">//&#x5E7F;&#x64AD; &#x4E00;&#x6761;&#x6D88;&#x606F;&#x591A;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x540C;&#x65F6;&#x6D88;&#x8D39;</span>
<span class="hljs-comment">//&#x53D1;&#x5E03;&#x6D88;&#x606F;</span>
channel.basicPublish(<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;&quot;</span>,<span class="hljs-keyword">null</span>,<span class="hljs-string">&quot;hello&quot;</span>.getBytes());
</code></pre>
<h5 id="2-&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1">2. &#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;fanout&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x5C06;&#x4E34;&#x65F6;&#x961F;&#x5217;&#x7ED1;&#x5B9A;exchange</span>
channel.queueBind(queue,<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;&quot;</span>);
<span class="hljs-comment">//&#x5904;&#x7406;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;1: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="3-&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2">3. &#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;fanout&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x5C06;&#x4E34;&#x65F6;&#x961F;&#x5217;&#x7ED1;&#x5B9A;exchange</span>
channel.queueBind(queue,<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;&quot;</span>);
<span class="hljs-comment">//&#x5904;&#x7406;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;2: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="4&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-3">4.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-3</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;fanout&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x5C06;&#x4E34;&#x65F6;&#x961F;&#x5217;&#x7ED1;&#x5B9A;exchange</span>
channel.queueBind(queue,<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;&quot;</span>);
<span class="hljs-comment">//&#x5904;&#x7406;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;3: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="5-&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;">5. &#x6D4B;&#x8BD5;&#x7ED3;&#x679C;</h5>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200315180653207.png" alt="image-20200315180653207"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200315180708489.png" alt="image-20200315180708489"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200315180728035.png" alt="image-20200315180728035"></p>
<hr>
<h3 id="46-&#x7B2C;&#x56DB;&#x79CD;&#x6A21;&#x578B;routing">4.6 &#x7B2C;&#x56DB;&#x79CD;&#x6A21;&#x578B;(Routing)</h3>
<h4 id="461-routing-&#x4E4B;&#x8BA2;&#x9605;&#x6A21;&#x578B;-direct&#x76F4;&#x8FDE;">4.6.1 Routing &#x4E4B;&#x8BA2;&#x9605;&#x6A21;&#x578B;-Direct(&#x76F4;&#x8FDE;)</h4>
<p><code>&#x5728;Fanout&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x4E00;&#x6761;&#x6D88;&#x606F;&#xFF0C;&#x4F1A;&#x88AB;&#x6240;&#x6709;&#x8BA2;&#x9605;&#x7684;&#x961F;&#x5217;&#x90FD;&#x6D88;&#x8D39;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5728;&#x67D0;&#x4E9B;&#x573A;&#x666F;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x4E0D;&#x540C;&#x7684;&#x6D88;&#x606F;&#x88AB;&#x4E0D;&#x540C;&#x7684;&#x961F;&#x5217;&#x6D88;&#x8D39;&#x3002;&#x8FD9;&#x65F6;&#x5C31;&#x8981;&#x7528;&#x5230;Direct&#x7C7B;&#x578B;&#x7684;Exchange&#x3002;</code></p>
<p> &#x5728;Direct&#x6A21;&#x578B;&#x4E0B;&#xFF1A;</p>
<ul>
<li>&#x961F;&#x5217;&#x4E0E;&#x4EA4;&#x6362;&#x673A;&#x7684;&#x7ED1;&#x5B9A;&#xFF0C;&#x4E0D;&#x80FD;&#x662F;&#x4EFB;&#x610F;&#x7ED1;&#x5B9A;&#x4E86;&#xFF0C;&#x800C;&#x662F;&#x8981;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;<code>RoutingKey</code>&#xFF08;&#x8DEF;&#x7531;key&#xFF09;</li>
<li>&#x6D88;&#x606F;&#x7684;&#x53D1;&#x9001;&#x65B9;&#x5728; &#x5411; Exchange&#x53D1;&#x9001;&#x6D88;&#x606F;&#x65F6;&#xFF0C;&#x4E5F;&#x5FC5;&#x987B;&#x6307;&#x5B9A;&#x6D88;&#x606F;&#x7684; <code>RoutingKey</code>&#x3002;</li>
<li>Exchange&#x4E0D;&#x518D;&#x628A;&#x6D88;&#x606F;&#x4EA4;&#x7ED9;&#x6BCF;&#x4E00;&#x4E2A;&#x7ED1;&#x5B9A;&#x7684;&#x961F;&#x5217;&#xFF0C;&#x800C;&#x662F;&#x6839;&#x636E;&#x6D88;&#x606F;&#x7684;<code>Routing Key</code>&#x8FDB;&#x884C;&#x5224;&#x65AD;&#xFF0C;&#x53EA;&#x6709;&#x961F;&#x5217;&#x7684;<code>Routingkey</code>&#x4E0E;&#x6D88;&#x606F;&#x7684; <code>Routing key</code>&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#xFF0C;&#x624D;&#x4F1A;&#x63A5;&#x6536;&#x5230;&#x6D88;&#x606F;</li>
</ul>
<p>&#x6D41;&#x7A0B;:</p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191126220145375.png" alt="image-20191126220145375"></p>
<p>&#x56FE;&#x89E3;&#xFF1A;</p>
<ul>
<li>P&#xFF1A;&#x751F;&#x4EA7;&#x8005;&#xFF0C;&#x5411;Exchange&#x53D1;&#x9001;&#x6D88;&#x606F;&#xFF0C;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x65F6;&#xFF0C;&#x4F1A;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;routing key&#x3002;</li>
<li>X&#xFF1A;Exchange&#xFF08;&#x4EA4;&#x6362;&#x673A;&#xFF09;&#xFF0C;&#x63A5;&#x6536;&#x751F;&#x4EA7;&#x8005;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x6D88;&#x606F;&#x9012;&#x4EA4;&#x7ED9; &#x4E0E;routing key&#x5B8C;&#x5168;&#x5339;&#x914D;&#x7684;&#x961F;&#x5217;</li>
<li>C1&#xFF1A;&#x6D88;&#x8D39;&#x8005;&#xFF0C;&#x5176;&#x6240;&#x5728;&#x961F;&#x5217;&#x6307;&#x5B9A;&#x4E86;&#x9700;&#x8981;routing key &#x4E3A; error &#x7684;&#x6D88;&#x606F;</li>
<li>C2&#xFF1A;&#x6D88;&#x8D39;&#x8005;&#xFF0C;&#x5176;&#x6240;&#x5728;&#x961F;&#x5217;&#x6307;&#x5B9A;&#x4E86;&#x9700;&#x8981;routing key &#x4E3A; info&#x3001;error&#x3001;warning &#x7684;&#x6D88;&#x606F;</li>
</ul>
<h5 id="1-&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">1. &#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x58F0;&#x660E;&#x4EA4;&#x6362;&#x673A;  &#x53C2;&#x6570;1:&#x4EA4;&#x6362;&#x673A;&#x540D;&#x79F0; &#x53C2;&#x6570;2:&#x4EA4;&#x6362;&#x673A;&#x7C7B;&#x578B; &#x57FA;&#x4E8E;&#x6307;&#x4EE4;&#x7684;Routing key&#x8F6C;&#x53D1;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;direct&quot;</span>);
String key = <span class="hljs-string">&quot;&quot;</span>;
<span class="hljs-comment">//&#x53D1;&#x5E03;&#x6D88;&#x606F;</span>
channel.basicPublish(<span class="hljs-string">&quot;logs_direct&quot;</span>,key,<span class="hljs-keyword">null</span>,(<span class="hljs-string">&quot;&#x6307;&#x5B9A;&#x7684;route key&quot;</span>+key+<span class="hljs-string">&quot;&#x7684;&#x6D88;&#x606F;&quot;</span>).getBytes());
</code></pre>
<h5 id="2&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1">2.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1</h5>
<pre><code class="lang-java"> <span class="hljs-comment">//&#x58F0;&#x660E;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;direct&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x961F;&#x5217;&#x548C;&#x4EA4;&#x6362;&#x673A;</span>
channel.queueBind(queue,<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;error&quot;</span>);
channel.queueBind(queue,<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;info&quot;</span>);
channel.queueBind(queue,<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;warn&quot;</span>);

<span class="hljs-comment">//&#x6D88;&#x8D39;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;1: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="3&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2">3.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x58F0;&#x660E;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;direct&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x961F;&#x5217;&#x548C;&#x4EA4;&#x6362;&#x673A;</span>
channel.queueBind(queue,<span class="hljs-string">&quot;logs_direct&quot;</span>,<span class="hljs-string">&quot;error&quot;</span>);
<span class="hljs-comment">//&#x6D88;&#x8D39;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;2: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="4&#x6D4B;&#x8BD5;&#x751F;&#x4EA7;&#x8005;&#x53D1;&#x9001;route-key&#x4E3A;error&#x7684;&#x6D88;&#x606F;&#x65F6;">4.&#x6D4B;&#x8BD5;&#x751F;&#x4EA7;&#x8005;&#x53D1;&#x9001;Route key&#x4E3A;error&#x7684;&#x6D88;&#x606F;&#x65F6;</h5>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200316102613933.png" alt="image-20200316102613933"></p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200316102627912.png" alt="image-20200316102627912"></p>
<h5 id="5&#x6D4B;&#x8BD5;&#x751F;&#x4EA7;&#x8005;&#x53D1;&#x9001;route-key&#x4E3A;info&#x7684;&#x6D88;&#x606F;&#x65F6;">5.&#x6D4B;&#x8BD5;&#x751F;&#x4EA7;&#x8005;&#x53D1;&#x9001;Route key&#x4E3A;info&#x7684;&#x6D88;&#x606F;&#x65F6;</h5>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200316102925740.png" alt="image-20200316102925740"></p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200316102947326.png" alt="image-20200316102947326"></p>
<hr>
<h4 id="462-routing-&#x4E4B;&#x8BA2;&#x9605;&#x6A21;&#x578B;-topic">4.6.2 Routing &#x4E4B;&#x8BA2;&#x9605;&#x6A21;&#x578B;-Topic</h4>
<p><code>Topic</code>&#x7C7B;&#x578B;&#x7684;<code>Exchange</code>&#x4E0E;<code>Direct</code>&#x76F8;&#x6BD4;&#xFF0C;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x6839;&#x636E;<code>RoutingKey</code>&#x628A;&#x6D88;&#x606F;&#x8DEF;&#x7531;&#x5230;&#x4E0D;&#x540C;&#x7684;&#x961F;&#x5217;&#x3002;&#x53EA;&#x4E0D;&#x8FC7;<code>Topic</code>&#x7C7B;&#x578B;<code>Exchange</code>&#x53EF;&#x4EE5;&#x8BA9;&#x961F;&#x5217;&#x5728;&#x7ED1;&#x5B9A;<code>Routing key</code> &#x7684;&#x65F6;&#x5019;&#x4F7F;&#x7528;&#x901A;&#x914D;&#x7B26;&#xFF01;&#x8FD9;&#x79CD;&#x6A21;&#x578B;<code>Routingkey</code> &#x4E00;&#x822C;&#x90FD;&#x662F;&#x7531;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5355;&#x8BCD;&#x7EC4;&#x6210;&#xFF0C;&#x591A;&#x4E2A;&#x5355;&#x8BCD;&#x4E4B;&#x95F4;&#x4EE5;&#x201D;.&#x201D;&#x5206;&#x5272;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A; <code>item.insert</code></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20191127121900255.png" alt="image-20191127121900255"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># &#x7EDF;&#x914D;&#x7B26;</span>
<span class="hljs-code">        * (star) can substitute for exactly one word.    &#x5339;&#x914D;&#x4E0D;&#x591A;&#x4E0D;&#x5C11;&#x6070;&#x597D;1&#x4E2A;&#x8BCD;</span>
<span class="hljs-code">        # (hash) can substitute for zero or more words.  &#x5339;&#x914D;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8BCD;</span>
<span class="hljs-section"># &#x5982;:</span>
<span class="hljs-code">        audit.#    &#x5339;&#x914D;audit.irs.corporate&#x6216;&#x8005; audit.irs &#x7B49;</span>
<span class="hljs-code">    audit.*   &#x53EA;&#x80FD;&#x5339;&#x914D; audit.irs</span>
</code></pre>
<h5 id="1&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">1.&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-comment">//&#x751F;&#x547D;&#x4EA4;&#x6362;&#x673A;&#x548C;&#x4EA4;&#x6362;&#x673A;&#x7C7B;&#x578B; topic &#x4F7F;&#x7528;&#x52A8;&#x6001;&#x8DEF;&#x7531;(&#x901A;&#x914D;&#x7B26;&#x65B9;&#x5F0F;)</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;topics&quot;</span>,<span class="hljs-string">&quot;topic&quot;</span>);
String routekey = <span class="hljs-string">&quot;user.save&quot;</span>;<span class="hljs-comment">//&#x52A8;&#x6001;&#x8DEF;&#x7531;key</span>
<span class="hljs-comment">//&#x53D1;&#x5E03;&#x6D88;&#x606F;</span>
channel.basicPublish(<span class="hljs-string">&quot;topics&quot;</span>,routekey,<span class="hljs-keyword">null</span>,(<span class="hljs-string">&quot;&#x8FD9;&#x662F;&#x8DEF;&#x7531;&#x4E2D;&#x7684;&#x52A8;&#x6001;&#x8BA2;&#x9605;&#x6A21;&#x578B;,route key: [&quot;</span>+routekey+<span class="hljs-string">&quot;]&quot;</span>).getBytes());
</code></pre>
<h5 id="2&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1">2.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-1</h5>
<p><code>Routing Key&#x4E2D;&#x4F7F;&#x7528;*&#x901A;&#x914D;&#x7B26;&#x65B9;&#x5F0F;</code></p>
<pre><code class="lang-java"> <span class="hljs-comment">//&#x58F0;&#x660E;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;topics&quot;</span>,<span class="hljs-string">&quot;topic&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x961F;&#x5217;&#x4E0E;&#x4EA4;&#x6362;&#x673A;&#x5E76;&#x8BBE;&#x7F6E;&#x83B7;&#x53D6;&#x4EA4;&#x6362;&#x673A;&#x4E2D;&#x52A8;&#x6001;&#x8DEF;&#x7531;</span>
channel.queueBind(queue,<span class="hljs-string">&quot;topics&quot;</span>,<span class="hljs-string">&quot;user.*&quot;</span>);

<span class="hljs-comment">//&#x6D88;&#x8D39;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;1: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="3&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2">3.&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;-2</h5>
<p><code>Routing Key&#x4E2D;&#x4F7F;&#x7528;#&#x901A;&#x914D;&#x7B26;&#x65B9;&#x5F0F;</code></p>
<pre><code class="lang-java"><span class="hljs-comment">//&#x58F0;&#x660E;&#x4EA4;&#x6362;&#x673A;</span>
channel.exchangeDeclare(<span class="hljs-string">&quot;topics&quot;</span>,<span class="hljs-string">&quot;topic&quot;</span>);
<span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
String queue = channel.queueDeclare().getQueue();
<span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x961F;&#x5217;&#x4E0E;&#x4EA4;&#x6362;&#x673A;&#x5E76;&#x8BBE;&#x7F6E;&#x83B7;&#x53D6;&#x4EA4;&#x6362;&#x673A;&#x4E2D;&#x52A8;&#x6001;&#x8DEF;&#x7531;</span>
channel.queueBind(queue,<span class="hljs-string">&quot;topics&quot;</span>,<span class="hljs-string">&quot;user.#&quot;</span>);

<span class="hljs-comment">//&#x6D88;&#x8D39;&#x6D88;&#x606F;</span>
channel.basicConsume(queue,<span class="hljs-keyword">true</span>,<span class="hljs-keyword">new</span> DefaultConsumer(channel){
  <span class="hljs-meta">@Override</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, <span class="hljs-keyword">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x8D39;&#x8005;2: &quot;</span>+<span class="hljs-keyword">new</span> String(body));
  }
});
</code></pre>
<h5 id="4&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;">4.&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;</h5>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200316113935785.png" alt="image-20200316113935785"></p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200316114000459.png" alt="image-20200316114000459"></p>
<h2 id="5-springboot&#x4E2D;&#x4F7F;&#x7528;rabbitmq">5. SpringBoot&#x4E2D;&#x4F7F;&#x7528;RabbitMQ</h2>
<h3 id="50-&#x642D;&#x5EFA;&#x521D;&#x59CB;&#x73AF;&#x5883;">5.0 &#x642D;&#x5EFA;&#x521D;&#x59CB;&#x73AF;&#x5883;</h3>
<h5 id="1-&#x5F15;&#x5165;&#x4F9D;&#x8D56;">1. &#x5F15;&#x5165;&#x4F9D;&#x8D56;</h5>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-amqp<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<h5 id="2-&#x914D;&#x7F6E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;">2. &#x914D;&#x7F6E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</h5>
<pre><code class="lang-yml"><span class="hljs-attr">spring:</span>
<span class="hljs-attr">  application:</span>
<span class="hljs-attr">    name:</span> springboot_rabbitmq
<span class="hljs-attr">  rabbitmq:</span>
<span class="hljs-attr">    host:</span> <span class="hljs-number">10.15</span><span class="hljs-number">.0</span><span class="hljs-number">.9</span>
<span class="hljs-attr">    port:</span> <span class="hljs-number">5672</span>
<span class="hljs-attr">    username:</span> ems
<span class="hljs-attr">    password:</span> <span class="hljs-number">123</span>
<span class="hljs-attr">    virtual-host:</span> /ems
</code></pre>
<p><code>RabbitTemplate</code>  &#x7528;&#x6765;&#x7B80;&#x5316;&#x64CD;&#x4F5C;     &#x4F7F;&#x7528;&#x65F6;&#x5019;&#x76F4;&#x63A5;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x6CE8;&#x5165;&#x5373;&#x53EF;&#x4F7F;&#x7528;</p>
<h3 id="51-&#x7B2C;&#x4E00;&#x79CD;hello-world&#x6A21;&#x578B;&#x4F7F;&#x7528;">5.1 &#x7B2C;&#x4E00;&#x79CD;hello world&#x6A21;&#x578B;&#x4F7F;&#x7528;</h3>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;

<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testHello</span><span class="hljs-params">()</span></span>{
  rabbitTemplate.convertAndSend(<span class="hljs-string">&quot;hello&quot;</span>,<span class="hljs-string">&quot;hello world&quot;</span>);
}
</code></pre>
</li>
<li><h5 id="&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;">&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>
<span class="hljs-meta">@RabbitListener</span>(queuesToDeclare = <span class="hljs-meta">@Queue</span>(<span class="hljs-string">&quot;hello&quot;</span>))
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HelloCustomer</span> </span>{

    <span class="hljs-meta">@RabbitHandler</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive1</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message = &quot;</span> + message);
    }
}
</code></pre>
</li>
</ol>
<h3 id="52-&#x7B2C;&#x4E8C;&#x79CD;work&#x6A21;&#x578B;&#x4F7F;&#x7528;">5.2 &#x7B2C;&#x4E8C;&#x79CD;work&#x6A21;&#x578B;&#x4F7F;&#x7528;</h3>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;

<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testWork</span><span class="hljs-params">()</span></span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    rabbitTemplate.convertAndSend(<span class="hljs-string">&quot;work&quot;</span>,<span class="hljs-string">&quot;hello work!&quot;</span>);
  }
}
</code></pre>
</li>
<li><h5 id="&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;">&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WorkCustomer</span> </span>{
    <span class="hljs-meta">@RabbitListener</span>(queuesToDeclare = <span class="hljs-meta">@Queue</span>(<span class="hljs-string">&quot;work&quot;</span>))
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive1</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;work message1 = &quot;</span> + message);
    }

    <span class="hljs-meta">@RabbitListener</span>(queuesToDeclare = <span class="hljs-meta">@Queue</span>(<span class="hljs-string">&quot;work&quot;</span>))
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive2</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;work message2 = &quot;</span> + message);
    }
}
</code></pre>
<blockquote>
<p><code>&#x8BF4;&#x660E;:&#x9ED8;&#x8BA4;&#x5728;Spring AMQP&#x5B9E;&#x73B0;&#x4E2D;Work&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x5C31;&#x662F;&#x516C;&#x5E73;&#x8C03;&#x5EA6;,&#x5982;&#x679C;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x80FD;&#x8005;&#x591A;&#x52B3;&#x9700;&#x8981;&#x989D;&#x5916;&#x914D;&#x7F6E;</code></p>
</blockquote>
</li>
</ol>
<h3 id="53-fanout-&#x5E7F;&#x64AD;&#x6A21;&#x578B;">5.3 Fanout &#x5E7F;&#x64AD;&#x6A21;&#x578B;</h3>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;

<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testFanout</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
  rabbitTemplate.convertAndSend(<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-string">&quot;&quot;</span>,<span class="hljs-string">&quot;&#x8FD9;&#x662F;&#x65E5;&#x5FD7;&#x5E7F;&#x64AD;&quot;</span>);
}
</code></pre>
</li>
</ol>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;">&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FanoutCustomer</span> </span>{

    <span class="hljs-meta">@RabbitListener</span>(bindings = <span class="hljs-meta">@QueueBinding</span>(
            value = <span class="hljs-meta">@Queue</span>,
            exchange = <span class="hljs-meta">@Exchange</span>(name=<span class="hljs-string">&quot;logs&quot;</span>,type = <span class="hljs-string">&quot;fanout&quot;</span>)
    ))
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive1</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message1 = &quot;</span> + message);
    }

    <span class="hljs-meta">@RabbitListener</span>(bindings = <span class="hljs-meta">@QueueBinding</span>(
            value = <span class="hljs-meta">@Queue</span>, <span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x961F;&#x5217;</span>
            exchange = <span class="hljs-meta">@Exchange</span>(name=<span class="hljs-string">&quot;logs&quot;</span>,type = <span class="hljs-string">&quot;fanout&quot;</span>)  <span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x4EA4;&#x6362;&#x673A;&#x7C7B;&#x578B;</span>
    ))
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive2</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message2 = &quot;</span> + message);
    }
}
</code></pre>
</li>
</ol>
<h3 id="54-route-&#x8DEF;&#x7531;&#x6A21;&#x578B;">5.4 Route &#x8DEF;&#x7531;&#x6A21;&#x578B;</h3>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;

<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testDirect</span><span class="hljs-params">()</span></span>{
  rabbitTemplate.convertAndSend(<span class="hljs-string">&quot;directs&quot;</span>,<span class="hljs-string">&quot;error&quot;</span>,<span class="hljs-string">&quot;error &#x7684;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&quot;</span>);
}
</code></pre>
</li>
<li><h5 id="&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;">&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DirectCustomer</span> </span>{

    <span class="hljs-meta">@RabbitListener</span>(bindings ={
            <span class="hljs-meta">@QueueBinding</span>(
                    value = <span class="hljs-meta">@Queue</span>(),
                    key={<span class="hljs-string">&quot;info&quot;</span>,<span class="hljs-string">&quot;error&quot;</span>},
                    exchange = <span class="hljs-meta">@Exchange</span>(type = <span class="hljs-string">&quot;direct&quot;</span>,name=<span class="hljs-string">&quot;directs&quot;</span>)
            )})
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive1</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message1 = &quot;</span> + message);
    }

    <span class="hljs-meta">@RabbitListener</span>(bindings ={
            <span class="hljs-meta">@QueueBinding</span>(
                    value = <span class="hljs-meta">@Queue</span>(),
                    key={<span class="hljs-string">&quot;error&quot;</span>},
                    exchange = <span class="hljs-meta">@Exchange</span>(type = <span class="hljs-string">&quot;direct&quot;</span>,name=<span class="hljs-string">&quot;directs&quot;</span>)
            )})
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive2</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message2 = &quot;</span> + message);
    }
}
</code></pre>
</li>
</ol>
<h3 id="55-topic-&#x8BA2;&#x9605;&#x6A21;&#x578B;&#x52A8;&#x6001;&#x8DEF;&#x7531;&#x6A21;&#x578B;">5.5 Topic &#x8BA2;&#x9605;&#x6A21;&#x578B;(&#x52A8;&#x6001;&#x8DEF;&#x7531;&#x6A21;&#x578B;)</h3>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;">&#x5F00;&#x53D1;&#x751F;&#x4EA7;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;

<span class="hljs-comment">//topic</span>
<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testTopic</span><span class="hljs-params">()</span></span>{
  rabbitTemplate.convertAndSend(<span class="hljs-string">&quot;topics&quot;</span>,<span class="hljs-string">&quot;user.save.findAll&quot;</span>,<span class="hljs-string">&quot;user.save.findAll &#x7684;&#x6D88;&#x606F;&quot;</span>);
}
</code></pre>
</li>
</ol>
<ol>
<li><h5 id="&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;">&#x5F00;&#x53D1;&#x6D88;&#x8D39;&#x8005;</h5>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TopCustomer</span> </span>{
    <span class="hljs-meta">@RabbitListener</span>(bindings = {
            <span class="hljs-meta">@QueueBinding</span>(
                    value = <span class="hljs-meta">@Queue</span>,
                    key = {<span class="hljs-string">&quot;user.*&quot;</span>},
                    exchange = <span class="hljs-meta">@Exchange</span>(type = <span class="hljs-string">&quot;topic&quot;</span>,name = <span class="hljs-string">&quot;topics&quot;</span>)
            )
    })
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive1</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message1 = &quot;</span> + message);
    }

    <span class="hljs-meta">@RabbitListener</span>(bindings = {
            <span class="hljs-meta">@QueueBinding</span>(
                    value = <span class="hljs-meta">@Queue</span>,
                    key = {<span class="hljs-string">&quot;user.#&quot;</span>},
                    exchange = <span class="hljs-meta">@Exchange</span>(type = <span class="hljs-string">&quot;topic&quot;</span>,name = <span class="hljs-string">&quot;topics&quot;</span>)
            )
    })
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">receive2</span><span class="hljs-params">(String message)</span></span>{
        System.out.println(<span class="hljs-string">&quot;message2 = &quot;</span> + message);
    }
}
</code></pre>
<hr>
</li>
</ol>
<h2 id="6-mq&#x7684;&#x5E94;&#x7528;&#x573A;&#x666F;">6. MQ&#x7684;&#x5E94;&#x7528;&#x573A;&#x666F;</h2>
<h3 id="61-&#x5F02;&#x6B65;&#x5904;&#x7406;">6.1 &#x5F02;&#x6B65;&#x5904;&#x7406;</h3>
<p><code>&#x573A;&#x666F;&#x8BF4;&#x660E;&#xFF1A;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x53D1;&#x6CE8;&#x518C;&#x90AE;&#x4EF6;&#x548C;&#x6CE8;&#x518C;&#x77ED;&#x4FE1;,&#x4F20;&#x7EDF;&#x7684;&#x505A;&#x6CD5;&#x6709;&#x4E24;&#x79CD; 1.&#x4E32;&#x884C;&#x7684;&#x65B9;&#x5F0F; 2.&#x5E76;&#x884C;&#x7684;&#x65B9;&#x5F0F;</code></p>
<ul>
<li><p><code>&#x4E32;&#x884C;&#x65B9;&#x5F0F;:</code> &#x5C06;&#x6CE8;&#x518C;&#x4FE1;&#x606F;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;&#x540E;,&#x53D1;&#x9001;&#x6CE8;&#x518C;&#x90AE;&#x4EF6;,&#x518D;&#x53D1;&#x9001;&#x6CE8;&#x518C;&#x77ED;&#x4FE1;,&#x4EE5;&#x4E0A;&#x4E09;&#x4E2A;&#x4EFB;&#x52A1;&#x5168;&#x90E8;&#x5B8C;&#x6210;&#x540E;&#x624D;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002; &#x8FD9;&#x6709;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x662F;,&#x90AE;&#x4EF6;,&#x77ED;&#x4FE1;&#x5E76;&#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x7684;,&#x5B83;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x901A;&#x77E5;,&#x800C;&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#x8BA9;&#x5BA2;&#x6237;&#x7AEF;&#x7B49;&#x5F85;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x7B49;&#x5F85;&#x7684;&#x4E1C;&#x897F;. </p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/SouthEast-4860248.png" alt="&#x8FD9;&#x91CC;&#x5199;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
</li>
<li><p><code>&#x5E76;&#x884C;&#x65B9;&#x5F0F;:</code>&#x5C06;&#x6CE8;&#x518C;&#x4FE1;&#x606F;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;&#x540E;,&#x53D1;&#x9001;&#x90AE;&#x4EF6;&#x7684;&#x540C;&#x65F6;,&#x53D1;&#x9001;&#x77ED;&#x4FE1;,&#x4EE5;&#x4E0A;&#x4E09;&#x4E2A;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x540E;,&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;,&#x5E76;&#x884C;&#x7684;&#x65B9;&#x5F0F;&#x80FD;&#x63D0;&#x9AD8;&#x5904;&#x7406;&#x7684;&#x65F6;&#x95F4;&#x3002; </p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/SouthEast-20191127211112660.png" alt="&#x8FD9;&#x91CC;&#x5199;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
</li>
<li><p><code>&#x6D88;&#x606F;&#x961F;&#x5217;:</code>&#x5047;&#x8BBE;&#x4E09;&#x4E2A;&#x4E1A;&#x52A1;&#x8282;&#x70B9;&#x5206;&#x522B;&#x4F7F;&#x7528;50ms,&#x4E32;&#x884C;&#x65B9;&#x5F0F;&#x4F7F;&#x7528;&#x65F6;&#x95F4;150ms,&#x5E76;&#x884C;&#x4F7F;&#x7528;&#x65F6;&#x95F4;100ms&#x3002;&#x867D;&#x7136;&#x5E76;&#x884C;&#x5DF2;&#x7ECF;&#x63D0;&#x9AD8;&#x7684;&#x5904;&#x7406;&#x65F6;&#x95F4;,&#x4F46;&#x662F;,&#x524D;&#x9762;&#x8BF4;&#x8FC7;,&#x90AE;&#x4EF6;&#x548C;&#x77ED;&#x4FE1;&#x5BF9;&#x6211;&#x6B63;&#x5E38;&#x7684;&#x4F7F;&#x7528;&#x7F51;&#x7AD9;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5F71;&#x54CD;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x7B49;&#x7740;&#x5176;&#x53D1;&#x9001;&#x5B8C;&#x6210;&#x624D;&#x663E;&#x793A;&#x6CE8;&#x518C;&#x6210;&#x529F;,&#x5E94;&#x8BE5;&#x662F;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;&#x540E;&#x5C31;&#x8FD4;&#x56DE;.  <code>&#x6D88;&#x606F;&#x961F;&#x5217;</code>: &#x5F15;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;&#x540E;&#xFF0C;&#x628A;&#x53D1;&#x9001;&#x90AE;&#x4EF6;,&#x77ED;&#x4FE1;&#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x5F02;&#x6B65;&#x5904;&#x7406; </p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/592892-20190520220249900-1679743651.jpg" alt="img"></p>
</li>
</ul>
<p>&#x7531;&#x6B64;&#x53EF;&#x4EE5;&#x770B;&#x51FA;,&#x5F15;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;&#x540E;&#xFF0C;&#x7528;&#x6237;&#x7684;&#x54CD;&#x5E94;&#x65F6;&#x95F4;&#x5C31;&#x7B49;&#x4E8E;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65F6;&#x95F4;+&#x5199;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;&#x7684;&#x65F6;&#x95F4;(&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x4E0D;&#x8BA1;),&#x5F15;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;&#x540E;&#x5904;&#x7406;&#x540E;,&#x54CD;&#x5E94;&#x65F6;&#x95F4;&#x662F;&#x4E32;&#x884C;&#x7684;3&#x500D;,&#x662F;&#x5E76;&#x884C;&#x7684;2&#x500D;&#x3002;</p>
<h3 id="62-&#x5E94;&#x7528;&#x89E3;&#x8026;">6.2 &#x5E94;&#x7528;&#x89E3;&#x8026;</h3>
<p><code>&#x573A;&#x666F;&#xFF1A;&#x53CC;11&#x662F;&#x8D2D;&#x7269;&#x72C2;&#x8282;,&#x7528;&#x6237;&#x4E0B;&#x5355;&#x540E;,&#x8BA2;&#x5355;&#x7CFB;&#x7EDF;&#x9700;&#x8981;&#x901A;&#x77E5;&#x5E93;&#x5B58;&#x7CFB;&#x7EDF;,&#x4F20;&#x7EDF;&#x7684;&#x505A;&#x6CD5;&#x5C31;&#x662F;&#x8BA2;&#x5355;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x5E93;&#x5B58;&#x7CFB;&#x7EDF;&#x7684;&#x63A5;&#x53E3;.</code></p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/SouthEast-20191127211247287.png" alt="&#x8FD9;&#x91CC;&#x5199;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
<p>&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#x6709;&#x4E00;&#x4E2A;&#x7F3A;&#x70B9;:</p>
<p>&#x5F53;&#x5E93;&#x5B58;&#x7CFB;&#x7EDF;&#x51FA;&#x73B0;&#x6545;&#x969C;&#x65F6;,&#x8BA2;&#x5355;&#x5C31;&#x4F1A;&#x5931;&#x8D25;&#x3002; &#x8BA2;&#x5355;&#x7CFB;&#x7EDF;&#x548C;&#x5E93;&#x5B58;&#x7CFB;&#x7EDF;&#x9AD8;&#x8026;&#x5408;.  &#x5F15;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217; </p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/SouthEast-20191127211304085.png" alt="&#x8FD9;&#x91CC;&#x5199;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
<ul>
<li><p><code>&#x8BA2;&#x5355;&#x7CFB;&#x7EDF;:</code>&#x7528;&#x6237;&#x4E0B;&#x5355;&#x540E;,&#x8BA2;&#x5355;&#x7CFB;&#x7EDF;&#x5B8C;&#x6210;&#x6301;&#x4E45;&#x5316;&#x5904;&#x7406;,&#x5C06;&#x6D88;&#x606F;&#x5199;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;,&#x8FD4;&#x56DE;&#x7528;&#x6237;&#x8BA2;&#x5355;&#x4E0B;&#x5355;&#x6210;&#x529F;&#x3002;</p>
</li>
<li><p><code>&#x5E93;&#x5B58;&#x7CFB;&#x7EDF;:</code>&#x8BA2;&#x9605;&#x4E0B;&#x5355;&#x7684;&#x6D88;&#x606F;,&#x83B7;&#x53D6;&#x4E0B;&#x5355;&#x6D88;&#x606F;,&#x8FDB;&#x884C;&#x5E93;&#x64CD;&#x4F5C;&#x3002;  &#x5C31;&#x7B97;&#x5E93;&#x5B58;&#x7CFB;&#x7EDF;&#x51FA;&#x73B0;&#x6545;&#x969C;,&#x6D88;&#x606F;&#x961F;&#x5217;&#x4E5F;&#x80FD;&#x4FDD;&#x8BC1;&#x6D88;&#x606F;&#x7684;&#x53EF;&#x9760;&#x6295;&#x9012;,&#x4E0D;&#x4F1A;&#x5BFC;&#x81F4;&#x6D88;&#x606F;&#x4E22;&#x5931;.</p>
</li>
</ul>
<h3 id="63-&#x6D41;&#x91CF;&#x524A;&#x5CF0;">6.3 &#x6D41;&#x91CF;&#x524A;&#x5CF0;</h3>
<p> <code>&#x573A;&#x666F;:</code> &#x79D2;&#x6740;&#x6D3B;&#x52A8;&#xFF0C;&#x4E00;&#x822C;&#x4F1A;&#x56E0;&#x4E3A;&#x6D41;&#x91CF;&#x8FC7;&#x5927;&#xFF0C;&#x5BFC;&#x81F4;&#x5E94;&#x7528;&#x6302;&#x6389;,&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x4E00;&#x822C;&#x5728;&#x5E94;&#x7528;&#x524D;&#x7AEF;&#x52A0;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;&#x3002;  </p>
<p>  <code>&#x4F5C;&#x7528;:</code> </p>
<p>&#x200B;            1.&#x53EF;&#x4EE5;&#x63A7;&#x5236;&#x6D3B;&#x52A8;&#x4EBA;&#x6570;&#xFF0C;&#x8D85;&#x8FC7;&#x6B64;&#x4E00;&#x5B9A;&#x9600;&#x503C;&#x7684;&#x8BA2;&#x5355;&#x76F4;&#x63A5;&#x4E22;&#x5F03;(&#x6211;&#x4E3A;&#x4EC0;&#x4E48;&#x79D2;&#x6740;&#x4E00;&#x6B21;&#x90FD;&#x6CA1;&#x6709;&#x6210;&#x529F;&#x8FC7;&#x5462;^^) </p>
<p>&#x200B;            2.&#x53EF;&#x4EE5;&#x7F13;&#x89E3;&#x77ED;&#x65F6;&#x95F4;&#x7684;&#x9AD8;&#x6D41;&#x91CF;&#x538B;&#x57AE;&#x5E94;&#x7528;(&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6309;&#x81EA;&#x5DF1;&#x7684;&#x6700;&#x5927;&#x5904;&#x7406;&#x80FD;&#x529B;&#x83B7;&#x53D6;&#x8BA2;&#x5355;) </p>
<p> <img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/SouthEast-20191127211341601.png" alt="&#x8FD9;&#x91CC;&#x5199;&#x56FE;&#x7247;&#x63CF;&#x8FF0;"></p>
<p>1.&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;,&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x4E4B;&#x540E;,&#x9996;&#x5148;&#x5199;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;,&#x52A0;&#x5165;&#x6D88;&#x606F;&#x961F;&#x5217;&#x957F;&#x5EA6;&#x8D85;&#x8FC7;&#x6700;&#x5927;&#x503C;,&#x5219;&#x76F4;&#x63A5;&#x629B;&#x5F03;&#x7528;&#x6237;&#x8BF7;&#x6C42;&#x6216;&#x8DF3;&#x8F6C;&#x5230;&#x9519;&#x8BEF;&#x9875;&#x9762;.  </p>
<p>2.&#x79D2;&#x6740;&#x4E1A;&#x52A1;&#x6839;&#x636E;&#x6D88;&#x606F;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x8BF7;&#x6C42;&#x4FE1;&#x606F;&#xFF0C;&#x518D;&#x505A;&#x540E;&#x7EED;&#x5904;&#x7406;.</p>
<hr>
<h2 id="7-rabbitmq&#x7684;&#x96C6;&#x7FA4;">7. RabbitMQ&#x7684;&#x96C6;&#x7FA4;</h2>
<h3 id="71-&#x96C6;&#x7FA4;&#x67B6;&#x6784;">7.1 &#x96C6;&#x7FA4;&#x67B6;&#x6784;</h3>
<h4 id="711-&#x666E;&#x901A;&#x96C6;&#x7FA4;&#x526F;&#x672C;&#x96C6;&#x7FA4;">7.1.1 &#x666E;&#x901A;&#x96C6;&#x7FA4;(&#x526F;&#x672C;&#x96C6;&#x7FA4;)</h4>
<blockquote>
<p>All data/state required for the operation of a RabbitMQ broker is replicated across all nodes. An exception to this are message queues, which by default reside on one node, though they are visible and reachable from all nodes. To replicate queues across nodes in a cluster   --&#x6458;&#x81EA;&#x5B98;&#x7F51;</p>
</blockquote>
<p><code>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;:RabbitMQ&#x4EE3;&#x7406;&#x64CD;&#x4F5C;&#x6240;&#x9700;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;/&#x72B6;&#x6001;&#x90FD;&#x5C06;&#x8DE8;&#x6240;&#x6709;&#x8282;&#x70B9;&#x590D;&#x5236;&#x3002;&#x8FD9;&#x65B9;&#x9762;&#x7684;&#x4E00;&#x4E2A;&#x4F8B;&#x5916;&#x662F;&#x6D88;&#x606F;&#x961F;&#x5217;&#xFF0C;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6D88;&#x606F;&#x961F;&#x5217;&#x4F4D;&#x4E8E;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x4E0A;&#xFF0C;&#x5C3D;&#x7BA1;&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x4ECE;&#x6240;&#x6709;&#x8282;&#x70B9;&#x770B;&#x5230;&#x548C;&#x8BBF;&#x95EE;</code></p>
<ol>
<li><h5 id="&#x67B6;&#x6784;&#x56FE;">&#x67B6;&#x6784;&#x56FE;</h5>
</li>
</ol>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320094147471.png" alt="image-20200320094147471"></p>
<p>&#x200B;    &#x6838;&#x5FC3;&#x89E3;&#x51B3;&#x95EE;&#x9898;:  <code>&#x5F53;&#x96C6;&#x7FA4;&#x4E2D;&#x67D0;&#x4E00;&#x65F6;&#x523B;master&#x8282;&#x70B9;&#x5B95;&#x673A;,&#x53EF;&#x4EE5;&#x5BF9;Quene&#x4E2D;&#x4FE1;&#x606F;,&#x8FDB;&#x884C;&#x5907;&#x4EFD;</code></p>
<ol>
<li><h5 id="&#x96C6;&#x7FA4;&#x642D;&#x5EFA;">&#x96C6;&#x7FA4;&#x642D;&#x5EFA;</h5>
<pre><code class="lang-markdown"><span class="hljs-section"># 0.&#x96C6;&#x7FA4;&#x89C4;&#x5212;</span>
<span class="hljs-code">    node1: 10.15.0.3  mq1  master &#x4E3B;&#x8282;&#x70B9;</span>
<span class="hljs-code">    node2: 10.15.0.4  mq2  repl1  &#x526F;&#x672C;&#x8282;&#x70B9;</span>
<span class="hljs-code">    node3: 10.15.0.5  mq3  repl2  &#x526F;&#x672C;&#x8282;&#x70B9;</span>

<span class="hljs-section"># 1.&#x514B;&#x9686;&#x4E09;&#x53F0;&#x673A;&#x5668;&#x4E3B;&#x673A;&#x540D;&#x548C;ip&#x6620;&#x5C04;</span>
<span class="hljs-code">    vim /etc/hosts&#x52A0;&#x5165;:</span>
<span class="hljs-code">         10.15.0.3 mq1</span>
<span class="hljs-code">        10.15.0.4 mq2</span>
<span class="hljs-code">        10.15.0.5 mq3</span>
<span class="hljs-code">    node1: vim /etc/hostname &#x52A0;&#x5165;:  mq1</span>
<span class="hljs-code">    node2: vim /etc/hostname &#x52A0;&#x5165;:  mq2</span>
<span class="hljs-code">    node3: vim /etc/hostname &#x52A0;&#x5165;:  mq3</span>

<span class="hljs-section"># 2.&#x4E09;&#x4E2A;&#x673A;&#x5668;&#x5B89;&#x88C5;rabbitmq,&#x5E76;&#x540C;&#x6B65;cookie&#x6587;&#x4EF6;,&#x5728;node1&#x4E0A;&#x6267;&#x884C;:</span>
<span class="hljs-code">    scp /var/lib/rabbitmq/.erlang.cookie root@mq2:/var/lib/rabbitmq/</span>
<span class="hljs-code">    scp /var/lib/rabbitmq/.erlang.cookie root@mq3:/var/lib/rabbitmq/</span>

<span class="hljs-section"># 3.&#x67E5;&#x770B;cookie&#x662F;&#x5426;&#x4E00;&#x81F4;:</span>
<span class="hljs-code">    node1: cat /var/lib/rabbitmq/.erlang.cookie </span>
<span class="hljs-code">    node2: cat /var/lib/rabbitmq/.erlang.cookie </span>
<span class="hljs-code">    node3: cat /var/lib/rabbitmq/.erlang.cookie </span>

<span class="hljs-section"># 4.&#x540E;&#x53F0;&#x542F;&#x52A8;rabbitmq&#x6240;&#x6709;&#x8282;&#x70B9;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x547D;&#x4EE4;,&#x542F;&#x52A8;&#x6210;&#x529F;&#x8BBF;&#x95EE;&#x7BA1;&#x7406;&#x754C;&#x9762;:</span>
<span class="hljs-code">    rabbitmq-server -detached </span>

<span class="hljs-section"># 5.&#x5728;node2&#x548C;node3&#x6267;&#x884C;&#x52A0;&#x5165;&#x96C6;&#x7FA4;&#x547D;&#x4EE4;:</span>
<span class="hljs-code">    1.&#x5173;&#x95ED;       rabbitmqctl stop_app</span>
<span class="hljs-code">    2.&#x52A0;&#x5165;&#x96C6;&#x7FA4;    rabbitmqctl join_cluster rabbit@mq1</span>
<span class="hljs-code">    3.&#x542F;&#x52A8;&#x670D;&#x52A1;    rabbitmqctl start_app</span>

<span class="hljs-section"># 6.&#x67E5;&#x770B;&#x96C6;&#x7FA4;&#x72B6;&#x6001;,&#x4EFB;&#x610F;&#x8282;&#x70B9;&#x6267;&#x884C;:</span>
<span class="hljs-code">    rabbitmqctl cluster_status</span>

<span class="hljs-section"># 7.&#x5982;&#x679C;&#x51FA;&#x73B0;&#x5982;&#x4E0B;&#x663E;&#x793A;,&#x96C6;&#x7FA4;&#x642D;&#x5EFA;&#x6210;&#x529F;:</span>
<span class="hljs-code">    Cluster status of node rabbit@mq3 ...</span>
<span class="hljs-code">    [{nodes,[{disc,[rabbit@mq1,rabbit@mq2,rabbit@mq3]}]},</span>
<span class="hljs-code">    {running_nodes,[rabbit@mq1,rabbit@mq2,rabbit@mq3]},</span>
<span class="hljs-code">    {cluster_name,&lt;&lt;&quot;rabbit@mq1&quot;&gt;&gt;},</span>
<span class="hljs-code">    {partitions,[]},</span>
<span class="hljs-code">    {alarms,[{rabbit@mq1,[]},{rabbit@mq2,[]},{rabbit@mq3,[]}]}]</span>

<span class="hljs-section"># 8.&#x767B;&#x5F55;&#x7BA1;&#x7406;&#x754C;&#x9762;,&#x5C55;&#x793A;&#x5982;&#x4E0B;&#x72B6;&#x6001;:</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320095613586.png" alt="image-20200320095613586"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># 9.&#x6D4B;&#x8BD5;&#x96C6;&#x7FA4;&#x5728;node1&#x4E0A;,&#x521B;&#x5EFA;&#x961F;&#x5217;</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320095743935.png" alt="image-20200320095743935"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># 10.&#x67E5;&#x770B;node2&#x548C;node3&#x8282;&#x70B9;:</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320095827688.png" alt="image-20200320095827688"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320095843370.png" alt="image-20200320095843370"></p>
<pre><code class="lang-markdown"><span class="hljs-section"># 11.&#x5173;&#x95ED;node1&#x8282;&#x70B9;,&#x6267;&#x884C;&#x5982;&#x4E0B;&#x547D;&#x4EE4;,&#x67E5;&#x770B;node2&#x548C;node3:</span>
<span class="hljs-code">    rabbitmqctl stop_app</span>
</code></pre>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320100000347.png" alt="image-20200320100000347"></p>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320100010968.png" alt="image-20200320100010968"></p>
<hr>
</li>
</ol>
<h4 id="712-&#x955C;&#x50CF;&#x96C6;&#x7FA4;">7.1.2 &#x955C;&#x50CF;&#x96C6;&#x7FA4;</h4>
<blockquote>
<p>This guide covers mirroring (queue contents replication) of classic queues  --&#x6458;&#x81EA;&#x5B98;&#x7F51;</p>
<p>By default, contents of a queue within a RabbitMQ cluster are located on a single node (the node on which the queue was declared). This is in contrast to exchanges and bindings, which can always be considered to be on all nodes. Queues can optionally be made <em>mirrored</em> across multiple nodes. --&#x6458;&#x81EA;&#x5B98;&#x7F51;</p>
</blockquote>
<p><code>&#x955C;&#x50CF;&#x961F;&#x5217;&#x673A;&#x5236;&#x5C31;&#x662F;&#x5C06;&#x961F;&#x5217;&#x5728;&#x4E09;&#x4E2A;&#x8282;&#x70B9;&#x4E4B;&#x95F4;&#x8BBE;&#x7F6E;&#x4E3B;&#x4ECE;&#x5173;&#x7CFB;&#xFF0C;&#x6D88;&#x606F;&#x4F1A;&#x5728;&#x4E09;&#x4E2A;&#x8282;&#x70B9;&#x4E4B;&#x95F4;&#x8FDB;&#x884C;&#x81EA;&#x52A8;&#x540C;&#x6B65;&#xFF0C;&#x4E14;&#x5982;&#x679C;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x4E0D;&#x53EF;&#x7528;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x5BFC;&#x81F4;&#x6D88;&#x606F;&#x4E22;&#x5931;&#x6216;&#x670D;&#x52A1;&#x4E0D;&#x53EF;&#x7528;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x63D0;&#x5347;MQ&#x96C6;&#x7FA4;&#x7684;&#x6574;&#x4F53;&#x9AD8;&#x53EF;&#x7528;&#x6027;&#x3002;</code></p>
<ol>
<li><h5 id="&#x96C6;&#x7FA4;&#x67B6;&#x6784;&#x56FE;">&#x96C6;&#x7FA4;&#x67B6;&#x6784;&#x56FE;</h5>
<p><img src="RibbitMQ &#x5B9E;&#x6218;&#x6559;&#x7A0B;.assets/image-20200320113423235.png" alt="image-20200320113423235"></p>
</li>
</ol>
<ol>
<li><h5 id="&#x914D;&#x7F6E;&#x96C6;&#x7FA4;&#x67B6;&#x6784;">&#x914D;&#x7F6E;&#x96C6;&#x7FA4;&#x67B6;&#x6784;</h5>
<p>```markdown</p>
<h1 id="0&#x7B56;&#x7565;&#x8BF4;&#x660E;">0.&#x7B56;&#x7565;&#x8BF4;&#x660E;</h1>
<pre><code>rabbitmqctl set_policy [-p &lt;vhost&gt;] [--priority &lt;priority&gt;] [--apply-to &lt;apply-to&gt;] &lt;name&gt; &lt;pattern&gt;  &lt;definition&gt;
-p Vhost&#xFF1A; &#x53EF;&#x9009;&#x53C2;&#x6570;&#xFF0C;&#x9488;&#x5BF9;&#x6307;&#x5B9A;vhost&#x4E0B;&#x7684;queue&#x8FDB;&#x884C;&#x8BBE;&#x7F6E;
Name:     policy&#x7684;&#x540D;&#x79F0;
Pattern: queue&#x7684;&#x5339;&#x914D;&#x6A21;&#x5F0F;(&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;)
Definition&#xFF1A;&#x955C;&#x50CF;&#x5B9A;&#x4E49;&#xFF0C;&#x5305;&#x62EC;&#x4E09;&#x4E2A;&#x90E8;&#x5206;ha-mode, ha-params, ha-sync-mode
               ha-mode:&#x6307;&#x660E;&#x955C;&#x50CF;&#x961F;&#x5217;&#x7684;&#x6A21;&#x5F0F;&#xFF0C;&#x6709;&#x6548;&#x503C;&#x4E3A; all/exactly/nodes
                    all&#xFF1A;&#x8868;&#x793A;&#x5728;&#x96C6;&#x7FA4;&#x4E2D;&#x6240;&#x6709;&#x7684;&#x8282;&#x70B9;&#x4E0A;&#x8FDB;&#x884C;&#x955C;&#x50CF;
                    exactly&#xFF1A;&#x8868;&#x793A;&#x5728;&#x6307;&#x5B9A;&#x4E2A;&#x6570;&#x7684;&#x8282;&#x70B9;&#x4E0A;&#x8FDB;&#x884C;&#x955C;&#x50CF;&#xFF0C;&#x8282;&#x70B9;&#x7684;&#x4E2A;&#x6570;&#x7531;ha-params&#x6307;&#x5B9A;
                    nodes&#xFF1A;&#x8868;&#x793A;&#x5728;&#x6307;&#x5B9A;&#x7684;&#x8282;&#x70B9;&#x4E0A;&#x8FDB;&#x884C;&#x955C;&#x50CF;&#xFF0C;&#x8282;&#x70B9;&#x540D;&#x79F0;&#x901A;&#x8FC7;ha-params&#x6307;&#x5B9A;
             ha-params&#xFF1A;ha-mode&#x6A21;&#x5F0F;&#x9700;&#x8981;&#x7528;&#x5230;&#x7684;&#x53C2;&#x6570;
            ha-sync-mode&#xFF1A;&#x8FDB;&#x884C;&#x961F;&#x5217;&#x4E2D;&#x6D88;&#x606F;&#x7684;&#x540C;&#x6B65;&#x65B9;&#x5F0F;&#xFF0C;&#x6709;&#x6548;&#x503C;&#x4E3A;automatic&#x548C;manual
            priority&#xFF1A;&#x53EF;&#x9009;&#x53C2;&#x6570;&#xFF0C;policy&#x7684;&#x4F18;&#x5148;&#x7EA7;
</code></pre></li>
</ol>
<h1 id="1&#x67E5;&#x770B;&#x5F53;&#x524D;&#x7B56;&#x7565;">1.&#x67E5;&#x770B;&#x5F53;&#x524D;&#x7B56;&#x7565;</h1>
<pre><code>   rabbitmqctl list_policies
</code></pre><h1 id="2&#x6DFB;&#x52A0;&#x7B56;&#x7565;">2.&#x6DFB;&#x52A0;&#x7B56;&#x7565;</h1>
<pre><code>   rabbitmqctl set_policy ha-all &apos;^hello&apos; &apos;{&quot;ha-mode&quot;:&quot;all&quot;,&quot;ha-sync-mode&quot;:&quot;automatic&quot;}&apos; 
   &#x8BF4;&#x660E;:&#x7B56;&#x7565;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; &#x201C;^&#x201D; &#x8868;&#x793A;&#x6240;&#x6709;&#x5339;&#x914D;&#x6240;&#x6709;&#x961F;&#x5217;&#x540D;&#x79F0;  ^hello:&#x5339;&#x914D;hello&#x5F00;&#x5934;&#x961F;&#x5217;
</code></pre><h1 id="3&#x5220;&#x9664;&#x7B56;&#x7565;">3.&#x5220;&#x9664;&#x7B56;&#x7565;</h1>
<pre><code>   rabbitmqctl clear_policy ha-all
</code></pre><h1 id="4&#x6D4B;&#x8BD5;&#x96C6;&#x7FA4;">4.&#x6D4B;&#x8BD5;&#x96C6;&#x7FA4;</h1>
<p>   ```</p>
<hr>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Introduction">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"test","level":"1.2","depth":1,"previous":{"title":"Introduction","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"RibbitMQ.md","mtime":"2020-12-16T12:50:24.550Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-16T12:51:17.656Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

